首页 > 代码库 > Dialog和ListView,progressBar的结合使用

Dialog和ListView,progressBar的结合使用

今天看了体格Demos,使用Dialog和ListView结合使用。

这个样例让我掌握了自己定义Dialog的使用和自己定义Adapter的使用。

以下是实现的步骤以及代码实现。

DialogListView。


1、自己定义dialog(PickDialog类):设置context,title。

将xml中的布局文件增加到该类中
1.1 PickDialog继承自Dialog。写构造方法初始化context、title、pickDialogListener。
1.2 重写onCreate方法,将布局文件增加到java文件里
1.3 定义public void initListViewData(ArrayList<String> list)方法。设置布局可见。并将适配器增加到布局文件里。
2、定义pickDialogListener接口。创建一下方法:
public void onLeftBtnClick();
public void onRightBtnClick();
public void onListItemClick(int position, String string);
public void onListItemLongClick(int position, String string);
public void onCancel();
2、自己定义ListView适配器(DialogListViewAdapter类):继承自BaseAdapter。覆写当中的方法。


public DialogListViewAdapter(Context context, List<String> list){
this.context=context;
this.list=list;
}


@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}


@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}


@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Holder holder =null;
if(convertView==null){
convertView=LayoutInflater.from(context).inflate(R.layout.blend_dialog_list_item, null);
holder=new Holder();
holder.blend_dialog_list_item_textview=(TextView) convertView.findViewById(R.id.blend_dialog_list_item_textview);

convertView.setTag(holder);
}else{
holder=(Holder) convertView.getTag();
}
holder.blend_dialog_list_item_textview.setText(list.get(position));


return convertView;
}
class Holder{
TextView blend_dialog_list_item_textview;
}
3、在MainActivity中将内容显示出来,new Handler().postDelayed()方法延迟载入listView布局

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <Button
        android:id="@+id/start"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="開始"/>

</RelativeLayout>
pickdialog.xml

<?

xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/dialog_title" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <LinearLayout android:id="@+id/progress_layout" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content"> <ProgressBar android:layout_width="match_parent" android:layout_height="374dp" android:indeterminate="false" /> </LinearLayout> <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content"> <ListView android:id="@+id/listview_list" android:layout_width="fill_parent" android:layout_height="wrap_content"> </ListView> </LinearLayout> </LinearLayout>

listview_item.xml

<?xml version="1.0" encoding="utf-8"?

> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/textview" android:textColor="#000000" android:layout_width="wrap_content" android:layout_height="20dp" /> </LinearLayout>

MainActivity.java

package lzl.edu.com.dialogtest;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity {

    Button start;
    PickDialog pickDialog;
    List<String> list = new ArrayList<String>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        start = (Button)findViewById(R.id.start);
        for(int i=0;i<100;i++){
            list.add("第"+i+"条数据");
        }

        start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pickDialog =new PickDialog(MainActivity.this,"明天的太阳", new PickDialogListener() {
                    @Override
                    public void onLeftBtnClick() {

                    }

                    @Override
                    public void onRightBtnClick() {

                    }

                    @Override
                    public void onListItemClick(int position, String string) {
                        Toast.makeText(MainActivity.this,string,Toast.LENGTH_LONG).show();
                    }

                    @Override
                    public void onListItemLongClick(int position, String string) {

                    }

                    @Override
                    public void onCancel() {

                    }
                });
                pickDialog.show();
                //for test 延迟三秒载入
                new Handler().postDelayed(new Runnable() {

                    public void run() {
                        //execute the task
                        pickDialog.initListViewData(list);
                    }
                }, 3000);
            }
        });

    }


}

PickDialog.java

package lzl.edu.com.dialogtest;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by admin on 2015/8/18.
 */
public class PickDialog extends Dialog {

     Context context;
     String title;
    TextView dialog_title;
     LinearLayout progress_layout;
     ListView listview_list;
     PickDialogListener pickDialogListener;
    DialogListView dialogListView;
    List<String> items = new ArrayList<String>();

   public PickDialog(Context context,String title,PickDialogListener pickDialogListener){
       super(context);
       this.context = context;
       this.title = title;
       this.pickDialogListener = pickDialogListener;
   }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //将xml文件里的布局增加到 java文件里
        LayoutInflater inflater = LayoutInflater.from(context);
        LinearLayout layout = (LinearLayout)inflater.inflate(R.layout.pickdialog, null);
        dialog_title = (TextView)layout.findViewById(R.id.dialog_title);
        progress_layout = (LinearLayout)layout.findViewById(R.id.progress_layout);
        listview_list = (ListView)layout.findViewById(R.id.listview_list);


        //将布局设置到view中。
        this.setContentView(layout);
    }
    public void initListViewData(List<String> list){
        items = list;
        //将进度条设置不可见
        progress_layout.setVisibility(View.GONE);
        dialog_title.setText(title);
        listview_list.setVisibility(View.VISIBLE);
        dialogListView = new DialogListView(context,list);

        listview_list.setAdapter(dialogListView);
        //设置每一个元素的 点击事件
        listview_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?

> parent, View view, int position, long id) { if(pickDialogListener!=null) pickDialogListener.onListItemClick(position,items.get(position)); } }); } }

DialogListView.java

package lzl.edu.com.dialogtest;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by admin on 2015/8/18.
 */
public class DialogListView extends BaseAdapter {

    private Context context;
    private List<String> list = new ArrayList<String>();
    public DialogListView(Context context,List<String> list){
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //将list_item.xml中的布局载入到listview中
        Holder holder = null;
        if(convertView == null){
            holder = new Holder();
            convertView = LayoutInflater.from(context).inflate(R.layout.listview_item,null);
            holder.textview = (TextView)convertView.findViewById(R.id.textview);
            convertView.setTag(holder);
        }else{
            holder = (Holder)convertView.getTag();
        }
        //设置文本内容
        holder.textview.setText(list.get(position));
        return convertView;
    }
    class Holder{
        TextView textview;
    }
}
PickDialogListener.java

package lzl.edu.com.dialogtest;

/**
 * Created by admin on 2015/8/18.
 */
public interface PickDialogListener {
    public void onLeftBtnClick();
    public void onRightBtnClick();
    public void onListItemClick(int position, String string);
    public void onListItemLongClick(int position, String string);
    public void onCancel();
}

技术分享技术分享技术分享

Dialog和ListView,progressBar的结合使用