Skip to content

Commit

Permalink
ListView 总结
Browse files Browse the repository at this point in the history
  • Loading branch information
mahao committed May 17, 2016
1 parent 191dcb0 commit 39912ed
Show file tree
Hide file tree
Showing 10 changed files with 237 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
@@ -1 +1 @@
include ':app', ':systemwidgetdemo', ':customviewdemo', ':utils', ':animationdemo', ':designpattern', ':arithmetic'
include ':app', ':systemwidgetdemo', ':customviewdemo', ':utils', ':animationdemo', ':designpattern', ':arithmetic', ':architecture'
2 changes: 1 addition & 1 deletion systemwidgetdemo/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".recycleView.swiperefresh.SwipeRefreshActivity"
<activity android:name=".listView.ListViewActivity"
android:theme="@style/Theme.AppCompat.Light.NoActionBar"
>
<intent-filter>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package com.mahao.alex.systemwidgetdemo.listView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* Created by Alex_MaHao on 2016/5/17.
*/
public abstract class BaseAppAdapter<T> extends BaseAdapter {

/**
* 泛型,保存数据
*/
protected List<T> datas;

/**
* 构造方法,子类必须实现其构造方法,并初始化数据
* @param datas
*/
public BaseAppAdapter(List<T> datas) {
this.datas = datas;
}

@Override
public int getCount() {
return datas==null?0:datas.size();
}

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

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {

BaseViewHolder vHolder;

if(convertView==null){
convertView = LayoutInflater.from(parent.getContext()).inflate(getItemLayoutId(),parent,false);

vHolder = new BaseViewHolder(convertView);

convertView.setTag(vHolder);
}else{
vHolder = (BaseViewHolder) convertView.getTag();
}

/**
* 数据绑定的回调
*/
bindData(vHolder,datas.get(position));

return convertView;
}

/**
* 子类实现,获取item布局文件的id
* @return
*/
protected abstract int getItemLayoutId();

/**
* 子类实现,绑定数据
* @param vHolder 对应position的ViewHolder
* @param data 对应的数据绑定
*/
protected abstract void bindData(BaseViewHolder vHolder,T data);


/**
* ViewHolder类
*/
class BaseViewHolder{

/**
* 保存view,以id-view的形式
*/
Map<Integer,View> mapView;

/**
* 根视图
*/
View rootView;

public BaseViewHolder(View rootView){
this.rootView = rootView;
mapView = new HashMap<Integer,View>();
}

/**
* 通过id查找控件
* @param id
* @return
*/
public View getView(Integer id){
View view = mapView.get(id);
if(view==null){
view = rootView.findViewById(id);
mapView.put(id,view);
}

return view;
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.mahao.alex.systemwidgetdemo.listView;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;

import com.mahao.alex.systemwidgetdemo.R;

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

/**
*
*
* ListView使用总结
* Created by Alex_MaHao on 2016/5/17.
*/
public class ListViewActivity extends AppCompatActivity {

private ListView mLv;

private List<String> datas = new ArrayList<>();

private SimpleBaseAdapter mAdapter;

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

setContentView(R.layout.activity_listview);

mLv = ((ListView) findViewById(R.id.listview_lv));

initDatas();

mAdapter = new SimpleBaseAdapter(datas);

mLv.setAdapter(mAdapter);

}

private void initDatas() {
for(int i = 0;i<20;i++){
datas.add("添加数据~~"+i);
}
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.mahao.alex.systemwidgetdemo.listView;

import android.widget.TextView;

import com.mahao.alex.systemwidgetdemo.R;

import java.util.List;

/**
* 最基础的adapter
* Created by Alex_MaHao on 2016/5/17.
*/

public class SimpleBaseAdapter extends BaseAppAdapter<String> {

public SimpleBaseAdapter(List<String> datas) {
super(datas);
}

@Override
protected int getItemLayoutId() {
return R.layout.item_listview_sample;
}

@Override
protected void bindData(BaseViewHolder vHolder, String data) {

((TextView) vHolder.getView(R.id.listview_sample_tv)).setText(data);

}


}
16 changes: 16 additions & 0 deletions systemwidgetdemo/src/main/res/layout/activity_listview.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<ListView
android:id="@+id/listview_lv"
android:divider="#f00"
android:scrollbars="none"
android:listSelector="#0000"
android:dividerHeight="3dp"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>


</LinearLayout>
13 changes: 13 additions & 0 deletions systemwidgetdemo/src/main/res/layout/item_listview_sample.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/listview_sample_tv"
android:layout_margin="10dp"
android:textColor="#000"
android:textSize="16sp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

</LinearLayout>

0 comments on commit 39912ed

Please sign in to comment.