Summary of commonly used Android ListView tips
- 2021-06-28 13:54:51
- OfStack
ListView's position in our Android project is witnessed, and we believe that almost every App has its own presence.
ListView mainly loads data in the form of a list, under specific circumstances, it needs to achieve some special functions: refreshing data, loading data, achieving animation effects, and so on.
As our common control, what should we pay attention to?
** Set a divider for every 1Item of ListView
Method 1: It is also the easiest way to set ListView's
divider attribute
For example: android:divider="@color/black"
Method 2: Set android:divider="@null" to indicate no separators, and then add them to the Item layout yourself.
<ListView>
android:id="@+id/test_lv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#d1d1d1"<!-- Set Separator Line Color -->
android:dividerHeight="1px"<!-- Set divider height -->
>
</ListView>
** By default, ListView's item has a somewhat clicking effect. How can I change this effect?
Set the android:listSelector property of ListViewr, such as setting Transparent Cancel effect android:listSelector="@android:color/transparent"
You can also set the effect you want by adding the corresponding color resource or drawble resource.
** When there is a lot of data, ListView will display a default scrollbar when scrolling. To cancel this scrollbar, you can set the android:scrollbars property
For example: android:scrollbars="none"
** ListView performance optimization, ViewHolder must be used to fully utilize recycle mechanism of ListView
** Dynamic data changes in ListView, add or delete data operations, the effect is obvious.
// Realization ListView Data Add Delete
public class MainActivity extends Activity implements View.OnClickListener {
private ListView test_lv;
private List<TestBean> dataList;
private TestAdapter adapter;
private Button add_btn, del_btn;
private ImageView emptyIv;
private LinearLayout operator_ll;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initDatas();
initViews();
}
private void initDatas() {
dataList = new ArrayList<>();
for (int i = 0; i < 5; i++) {
TestBean bean = new TestBean();
bean.setTitle(" Title _" + i);
bean.setContent(" This is the content _" + i);
bean.setType(i % 2 == 0 ? 1 : 2);
dataList.add(bean);
}
}
private void initViews() {
this.test_lv = (ListView) findViewById(R.id.test_lv);
this.add_btn = (Button) findViewById(R.id.add_btn);
this.del_btn = (Button) findViewById(R.id.del_btn);
emptyIv = (ImageView) findViewById(R.id.empty_iv);
operator_ll=(LinearLayout)findViewById(R.id.operator_ll);
this.add_btn.setOnClickListener(this);
this.del_btn.setOnClickListener(this);
if (dataList.size() == 0) {
emptyIv.setVisibility(View.VISIBLE);
operator_ll.setVisibility(View.GONE);
test_lv.setEmptyView(emptyIv);
} else {
emptyIv.setVisibility(View.GONE);
operator_ll.setVisibility(View.VISIBLE);
adapter = new TestAdapter(this, dataList);
test_lv.setAdapter(adapter);
}
test_lv.setSelection(15);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.add_btn:
// Increase by default at 1 Locations
TestBean bean = new TestBean();
bean.setTitle(" Additions ");
bean.setContent(" This is an addition ");
bean.setType(1);
dataList.add(0, bean);
break;
case R.id.del_btn:
// Delete by default 1 Bar data
dataList.remove(0);
break;
}
adapter.notifyDataSetChanged();// Refresh ListView data
}
}
//ListView Corresponding Adapter
public class TestAdapter extends BaseAdapter {
private Context mContext;
private List<TestBean> listDatas;
public TestAdapter(Context mContext, List<TestBean> listDatas) {
this.mContext = mContext;
this.listDatas = listDatas;
}
@Override
public int getCount() {
return listDatas.size();
}
@Override
public Object getItem(int position) {
return listDatas.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(mContext).inflate(R.layout.test_item, null);
holder.title = (TextView) convertView.findViewById(R.id.item_title);
holder.content = (TextView) convertView.findViewById(R.id.item_content);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.content.setText(listDatas.get(position).getContent());
holder.title.setText(listDatas.get(position).getTitle());
return convertView;
}
final class ViewHolder {
TextView title;
TextView content;
}
}
** Similar to chat interface, ListView has multiple item style effects
// Realization ListView Medium variety item style
public class TypeActivity extends Activity {
private ListView type_lv;
private List<TestBean> dataList;
private TypeAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_type);
initDatas();
initViews();
}
private void initDatas() {
dataList = new ArrayList<>();
for (int i = 0; i < 9; i++) {
TestBean bean = new TestBean();
bean.setTitle(" Title _" + i);
bean.setContent(" This is the content _" + i);
bean.setType(i % 2 == 0 ? 1 : 2);
dataList.add(bean);
}
}
private void initViews() {
this.type_lv = (ListView) findViewById(R.id.type_lv);
adapter = new TypeAdapter(this, dataList);
type_lv.setAdapter(adapter);
}
}
// Corresponding Adapter
public class TypeAdapter extends BaseAdapter {
private Context mContext;
private List<TestBean> listDatas;
public TypeAdapter(Context mContext, List<TestBean> listDatas) {
this.mContext = mContext;
this.listDatas = listDatas;
}
@Override
public int getCount() {
return listDatas.size();
}
@Override
public Object getItem(int position) {
return listDatas.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
// Set different layouts and data presentations according to style
int type = getItemViewType(position);
if (convertView == null) {
holder = new ViewHolder();
if (type == 1) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.test_item, null);
holder.title = (TextView) convertView.findViewById(R.id.item_title);
holder.content = (TextView) convertView.findViewById(R.id.item_content);
} else {
convertView = LayoutInflater.from(mContext).inflate(R.layout.type_item, null);
holder.title = (TextView) convertView.findViewById(R.id.type_title);
holder.content = (TextView) convertView.findViewById(R.id.type_title);
}
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.content.setText(listDatas.get(position).getContent());
holder.title.setText(listDatas.get(position).getTitle());
return convertView;
}
// Key Methods getViewTypeCount : Get how many styles
@Override
public int getViewTypeCount() {
return 2;
}
// Key Methods getItemViewType : Get item type
@Override
public int getItemViewType(int position) {
return listDatas.get(position).getType();
}
final class ViewHolder {
TextView title;
TextView content;
}
}
Related Layout
activity_main.xml
<?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">
<LinearLayout
android:id="@+id/operator_ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/add_btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text=" Add to " />
<Button
android:id="@+id/del_btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text=" delete " />
</LinearLayout>
<ListView
android:id="@+id/test_lv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#d1d1d1"<!-- Separator Line Color -->
android:dividerHeight="1px"<!-- Divider Height -->
android:listSelector="@android:color/transparent"<!-- Cancel default click effect -->
android:scrollbars="none"><!-- Hide scrollbars -->
</ListView>
<ImageView
android:id="@+id/empty_iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="100dp"
android:src="@mipmap/empty"
android:visibility="gone" />
</LinearLayout>
activity_type.xml
<?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">
<ListView
android:id="@+id/type_lv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
></ListView>
</LinearLayout>
test_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="wrap_content"
android:orientation="vertical"
android:paddingLeft="15dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp">
<TextView
android:id="@+id/item_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<TextView
android:id="@+id/item_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="Hello World!" />
</LinearLayout>
This is the complete collection of commonly used Android ListView tips, hope to help everyone learn.