直接上代码。
Adapter的封装
package com.jackeysun.touchpullview;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import java.util.ArrayList;
import java.util.List;
public abstract class AbsAdapter<T, H extends AbsViewHolder> extends BaseAdapter {
protected List<T> mDatas;
protected Context mContext;
protected LayoutInflater mLayoutInflater;
protected int layoutId;
protected H holder;
public AbsAdapter(Context context, int layoutId) {
this.mContext = context;
this.mDatas = new ArrayList<>();
this.layoutId = layoutId;
this.mLayoutInflater = LayoutInflater.from(context);
}
protected void setItem(List<T> mDatas) {
this.mDatas.clear();
for (T data : mDatas) {
this.mDatas.add(data);
}
this.notifyDataSetChanged();
}
protected void addItems(List<T> mDatas) {
for (T data : mDatas) {
addItem(data);
}
this.notifyDataSetChanged();
}
private void addItem(T data) {
mDatas.add(data);
}
@Override
public int getCount() {
return mDatas == null ? 0 : mDatas.size();
}
@Override
public Object getItem(int i) {
return mDatas.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
holder = null;
if (view == null) {
holder = newViewHolder();
view = mLayoutInflater.inflate(layoutId, null);
view.setTag(holder);
} else {
holder = (H) view.getTag();
}
holder.initItemView(view);
initData(mDatas.get(i), holder);
return view;
}
protected abstract void initData(T datas, H holder);
public abstract H newViewHolder();
}
- 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
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 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
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
ViewHolder的封装
package com.jackeysun.touchpullview;
import android.view.View;
/**
* Created by jackey on 2017/7/23.
*/
public abstract class AbsViewHolder {
public abstract void initItemView(View containerView);
}
使用方式
package com.jackeysun.touchpullview;
import Android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import Java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ListView listView;
private MyAdapter adapter;
private List<String> datas;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listView);
adapter = new MyAdapter(MainActivity.this, R.layout.item);
listView.setAdapter(adapter);
}
public void click(View view){
datas = new ArrayList<>();
for (int i = 0; i < 10; i++) {
datas.add(new String("第" + i + "项"));
}
adapter.setItem(datas);
}
}
package com.jackeysun.touchpullview;
import android.content.Context;
/**
* Created by jackey on 2017/7/23.
*/
public class MyAdapter extends AbsAdapter<String, MyViewHolder> {
public MyAdapter(Context context, int layoutId) {
super(context, layoutId);
}
@Override
protected void initData(String datas, MyViewHolder viewHolder) {
viewHolder.textView.setText(datas);
}
@Override
public MyViewHolder newViewHolder() {
return new MyViewHolder();
}
}
- 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
- 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
package com.jackeysun.touchpullview;
import android.view.View;
import android.widget.TextView;
/**
* Created by jackey on 2017/7/23.
*/
public class MyViewHolder extends AbsViewHolder {
protected TextView textView;
public MyViewHolder() {
}
@Override
public void initItemView(View containerView) {
textView = (TextView) containerView.findViewById(R.id.textView);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
<?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:orientation="vertical"
tools:context="com.jackeysun.touchpullview.MainActivity">
<Button
android:layout_width="match_parent"
android:layout_height="48dp"
android:onClick="click"
android:text="显示数据"/>
<ListView
android:id="@+id/listView"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:divider="#999"
android:dividerHeight="1dp"></ListView>
</LinearLayout>