上一次我用include实现的这个功能,这次用自定义布局来实现,
创建CompoundView类重写前三个构造方法
private void initView(Context mContext ){
LayoutInflater inflater=(LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//将compound_view导入LinearLayout中
inflater.inflate(R.layout.compound_view,this);
btn= (Button) this.findViewById(R.id.view_item_btn);
mProgressBar=(ProgressBar)this.findViewById(R.id.view_pbar);
}
在initView()方法中把自己的布局加载进去
定义
private int max=100;//最大进度
private int progress=0;//当前进度
private OnClickListener listener;
设置他们的set方法
//提供给使用者设置butter监听的
public void setListener(OnClickListener listener) {
this.listener = listener;
//为butter设置监听
btn.setOnClickListener(listener);
}
public void setMax(int max) {
this.max = max;
mProgressBar.setMax(max);
}
public void setProgress(int progress) {
if(progress>max){
this.progress=max;
}else {
this.progress = progress;
}
mProgressBar.setProgress(progress);
}
重写onFinishInflate
@Override
protected void onFinishInflate() {
super.onFinishInflate();
//设置进度条默认最大值
mProgressBar.setMax(max);
}
自定义布局中的代码就完了
在MainActivity中引入布局
设置默认的最大值和监听事件
myView.setMax(50);
myView.setListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
b=false;
i=0;
myView.setProgress(i);
}
});
全部代码
重写xml布局
compound_view
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="horizontal"
android:gravity="center"
android:padding="20dp"
android:layout_height="match_parent">
<ProgressBar
android:layout_width="5dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/view_pbar"
style="?android:attr/progressBarStyleHorizontal"
android:max="10"
/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/view_item_btn"
android:layout_weight="1"
android:text="取消下载"
/>
</merge>
mainActivity的布局代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/main_btn"
android:text="下载"
android:layout_height="wrap_content"
android:layout_width="match_parent"
/>
<com.axnet.viewapp.CompoundView
android:id="@+id/main_in"
android:layout_height="wrap_content"
android:layout_width="match_parent"
layout="@layout/main_item"
/>
</LinearLayout>
CompoundView代码
public class CompoundView extends LinearLayout{
private Button btn;
private ProgressBar mProgressBar;
private int max=100;//最大进度
private int progress=0;//当前进度
private OnClickListener listener;
public CompoundView(Context context) {
super(context);
initView(context);
}
public CompoundView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
initView(context);
}
public CompoundView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView(context);
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
//设置进度条默认最大值
mProgressBar.setMax(max);
}
//提供给使用者设置butter监听的
public void setListener(OnClickListener listener) {
this.listener = listener;
//为butter设置监听
btn.setOnClickListener(listener);
}
public void setMax(int max) {
this.max = max;
mProgressBar.setMax(max);
}
public void setProgress(int progress) {
if(progress>max){
this.progress=max;
}else {
this.progress = progress;
}
mProgressBar.setProgress(progress);
}
private void initView(Context mContext ){
LayoutInflater inflater=(LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//将compound_view导入LinearLayout中
inflater.inflate(R.layout.compound_view,this);
btn= (Button) this.findViewById(R.id.view_item_btn);
mProgressBar=(ProgressBar)this.findViewById(R.id.view_pbar);
}
}
mainActivity代码
public class MainActivity extends AppCompatActivity {
Button main_btn,main_item_btn;
CompoundView myView;
int i=0;
boolean b=true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initClick();
}
private void initClick() {
//下载按钮
main_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
b=true;
new Thread(new Runnable() {
@Override
public void run() {
while (b){
try {
Thread.sleep(1000);
runOnUiThread(new Runnable() {
@Override
public void run() {
myView.setProgress(i++);
}
});
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
});
myView.setMax(50);
myView.setListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
b=false;
i=0;
myView.setProgress(i);
}
});
}
private void initView() {
main_btn=(Button)findViewById(R.id.main_btn);
myView= (CompoundView) findViewById(R.id.main_in);
}
}