1. 程式人生 > >Dialog和ListView,progressBar的結合使用

Dialog和ListView,progressBar的結合使用

ati als 一個 tex stub onclick dsm 適配器 ttext

今天看了體格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的結合使用