学习android listview及其原生下拉刷新的笔记

listview的建立

1.定义一个Data实体类(作为listview适配器的适配类型)

代码如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class Data {
public String name;
public int imageId;
public Data(String name,int imageId)
{
this.name = name;
this.imageId = imageId;
}
public String getName(){
return name;
}
public int getImageId()
{
return imageId;
}
}

2.在layout目录下建立item.xml(用于加载listview中每一项的内容)

代码如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?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">
<ImageView
android:id="@+id/image"
android:layout_height="wrap_content"
android:layout_width="match_parent"
/>
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>

3.创建一个自定义的适配器,继承自Arraydapter,并将泛型指定为Data类

代码如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class DataAdapter extends ArrayAdapter{
private int resourceId;
public DataAdapter(Context context, int textViewResourceId, List<Data> object){
super(context,textViewResourceId,object);
resourceId = textViewResourceId;
}
@Override
public View getView(int position,View convertView,ViewGroup parent){
Data data = (Data) getItem(position);
View view = LayoutInflater.from(getContext()).inflate(resourceId,null);
ImageView dataimage = (ImageView)view.findViewById(R.id.image);
TextView dataName = (TextView)view.findViewById(R.id.text);
dataimage.setImageResource(data.getImageId());
dataName.setText(data.getName());
return view;
}
}

4.然后就是MainActivity

代码如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public class MainActivity extends AppCompatActivity {
private List<Data> dataList = new ArrayList<Data>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initData();
DataAdapter adapter = new DataAdapter(Main2Activity.this,R.layout.item,dataList);
ListView listview = (ListView)findViewById(R.id.listview1);
listview.setAdapter(adapter);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Data data = dataList.get(position);
Toast.makeText(MainActivity.this,data.getName(), Toast.LENGTH_LONG).show();
}
});
}
private void initData(){
Data biyadi = new Data("biyadi",R.drawable.biyadi);
dataList.add(biyadi);
Data falali = new Data("falali",R.drawable.falali);
dataList.add(falali);
}
}

5.最后别忘了在activity_main.xml中添加listview控件

代码如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.ding.listview_practice.MainActivity"
android:weightSum="1">
<ListView
android:id="@+id/listview1"
android:layout_height="wrap_content"
android:layout_width="match_parent"
>
</ListView>
</LinearLayout>

google原生下拉刷新

只需对上面的代码作如下修改

1.修改activity_main.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.ding.listview_practice.MainActivity"
android:weightSum="1">
<LinearLayout
android:layout_width="400dp"
android:layout_height="65dp">
<EditText
android:layout_height="wrap_content"
android:layout_width="339dp"/>
</LinearLayout>

<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/id_swipe_ly"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<ListView
android:id="@+id/listview1"
android:layout_height="wrap_content"
android:layout_width="match_parent"
>
</ListView>
</android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>

2.修改MainActivity

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
public class MainActivity extends Activity implements SwipeRefreshLayout.OnRefreshListener{
private static final int REFRESH_COMPLETE = 0X110;
private List<Data> dataList = new ArrayList<Data>();
private ArrayAdapter adapter;
private ListView listview;
private SwipeRefreshLayout mSwipeLayout;

private Handler mHandler = new Handler()
{
public void handleMessage(android.os.Message msg)
{
switch (msg.what)
{
case REFRESH_COMPLETE:
Data as = new Data("as",R.drawable.asd);
dataList.add(as);
adapter.notifyDataSetChanged();
mSwipeLayout.setRefreshing(false);
break;

}
};
};
@SuppressLint("InlinedApi")
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initdata();
adapter = new DataAdapter(MainActivity.this,R.layout.item,dataList);
listview = (ListView)findViewById(R.id.listview1);
mSwipeLayout = (SwipeRefreshLayout) findViewById(R.id.id_swipe_ly);
mSwipeLayout.setOnRefreshListener(this);
mSwipeLayout.setColorScheme(android.R.color.holo_blue_bright, android.R.color.holo_green_light,
android.R.color.holo_orange_light, android.R.color.holo_red_light);
listview.setAdapter(adapter);
}
public void onRefresh()
{
mHandler.sendEmptyMessageDelayed(REFRESH_COMPLETE, 2000);
}
private void initdata()
{
Data asd = new Data("asd",R.drawable.asd);
dataList.add(asd);
}
}
坚持原创技术分享,您的支持将鼓励我继续创作!