提交 fbba6c45 编写于 作者: Black_Hao's avatar Black_Hao

Add CommonBaseAdapter

Add CommonBaseAdapter
上级 d220009f
package com.balckhao.blackhaoutil.base;
import android.annotation.IdRes;
import android.content.Context;
import android.util.SparseArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import java.util.List;
/**
* Author : BlackHao
* Time : 2018/9/4 14:11
* Description : ListView/GridView BaseAdapter
*/
public abstract class CommonBaseAdapter<T> extends BaseAdapter {
private List<T> list;
private Context context;
public CommonBaseAdapter(List<T> list, Context context) {
this.list = list;
this.context = context;
}
@Override
public int getCount() {
return list == null ? 0 : list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@SuppressWarnings("unchecked")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = View.inflate(context, getLayoutResId(), null);
viewHolder = new ViewHolder();
//将控件与 ViewHolder 绑定
int[] viewIdArray = bindView();
for (int aViewIdArray : viewIdArray) {
viewHolder.bindViewById(convertView, aViewIdArray);
}
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
initData(viewHolder, list.get(position), position);
return convertView;
}
/**
* 获取 layout 文件
*
* @return layout 文件 ID
*/
protected abstract int getLayoutResId();
/**
* 将控件与 ViewHolder 绑定
*
* @return 需要绑定的控件 ID 数组
*/
protected abstract int[] bindView();
/**
* 绑定显示数据,增加回调监听等操作
*
* @param holder ViewHolder
* @param position 对应的位置
* @param t list.get(position)数据
*/
protected abstract void initData(ViewHolder holder, T t, int position);
public class ViewHolder {
private SparseArray<View> viewSparseArray;
ViewHolder() {
this.viewSparseArray = new SparseArray<>();
}
/**
* 通过 id获取 View
*
* @param id View ID
* @param <E> View 类型
* @return 对应的 View
*/
@SuppressWarnings("unchecked")
public <E extends View> E getViewById(@IdRes int id) {
return (E) viewSparseArray.get(id);
}
/**
* 通过 id 获取 View并绑定到 ViewHolder
*
* @param view 布局文件 View
* @param id View ID
*/
public void bindViewById(View view, @IdRes int id) {
viewSparseArray.put(id, view.findViewById(id));
}
}
}
......@@ -27,6 +27,7 @@ public class TestActivity extends BaseActivity {
protected void initUI() {
setContentView(R.layout.activity_base_test);
ButterKnife.bind(this);
//replaceFragment
replaceFragment(R.id.frame_1, new TestFragment());
replaceFragment(R.id.frame_2, new TestFragment());
}
......
package com.balckhao.blackhaoutil.base;
import android.content.Context;
import android.widget.ImageView;
import android.widget.TextView;
import com.balckhao.blackhaoutil.R;
import java.util.List;
/**
* Author : BlackHao
* Time : 2018/9/4 14:58
* Description :
*/
public class TestAdapter extends CommonBaseAdapter<String> {
public TestAdapter(List<String> list, Context context) {
super(list, context);
}
@Override
protected int getLayoutResId() {
return R.layout.adapter_test;
}
@Override
protected int[] bindView() {
return new int[]{R.id.test_iv, R.id.test_tv};
}
@Override
protected void initData(ViewHolder holder, String s, int position) {
TextView tv = holder.getViewById(R.id.test_tv);
ImageView iv = holder.getViewById(R.id.test_iv);
tv.setText("Position:" + position + " Content : " + s);
if (s.endsWith("pdf")) {
iv.setBackgroundResource(R.drawable.pdf);
} else if (s.endsWith("ppt")){
iv.setBackgroundResource(R.drawable.ppt);
}else {
iv.setBackgroundResource(R.drawable.unkown_file);
}
}
}
......@@ -2,9 +2,12 @@ package com.balckhao.blackhaoutil.base;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import com.balckhao.blackhaoutil.R;
import java.util.ArrayList;
import butterknife.Bind;
import butterknife.ButterKnife;
......@@ -18,6 +21,8 @@ public class TestFragment extends BaseFragment {
@Bind(R.id.iv_1)
ImageView iv1;
@Bind(R.id.lv_test)
ListView lvTest;
@Override
protected int initLayoutRes() {
......@@ -32,6 +37,17 @@ public class TestFragment extends BaseFragment {
@Override
protected void initData() {
ArrayList<String> arrayList = new ArrayList<>();
arrayList.add("Hello World, Android.ppt");
arrayList.add("Hello World, Java.pdf");
arrayList.add("Hello World, JS.pdf");
arrayList.add("Hello World, C.ppt");
arrayList.add("Hello World, C++");
arrayList.add("Hello World, C#.pdf");
arrayList.add("Hello World, Python.ppt");
arrayList.add("Hello World, PHP");
TestAdapter testAdapter = new TestAdapter(arrayList, getContext());
lvTest.setAdapter(testAdapter);
}
@Override
......@@ -44,4 +60,5 @@ public class TestFragment extends BaseFragment {
super.onDestroyView();
ButterKnife.unbind(this);
}
}
......@@ -14,7 +14,7 @@
<FrameLayout
android:id="@+id/frame_1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_height="500dp"
android:layout_marginTop="10dp" />
<!--分割线-->
......@@ -26,7 +26,7 @@
<FrameLayout
android:id="@+id/frame_2"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_height="500dp"
android:layout_marginTop="10dp" />
......
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/test_iv"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:layout_marginStart="20dp"
android:contentDescription="@null" />
<TextView
android:id="@+id/test_tv"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_centerVertical="true"
android:layout_marginLeft="70dp"
android:layout_marginStart="70dp"
android:gravity="center" />
</RelativeLayout>
\ No newline at end of file
......@@ -7,7 +7,14 @@
android:id="@+id/iv_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_centerHorizontal="true"
android:contentDescription="@null" />
<ListView
android:id="@+id/lv_test"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/iv_1"
android:layout_marginTop="10dp" />
</RelativeLayout>
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册