ListView分組顯示,設定懸浮條
阿新 • • 發佈:2019-02-01
最近好忙,沒時間看書,沒時間寫部落格。最近 做了個listview分組顯示的東西,發現ios那邊的ListView 頭可以
懸浮顯示,然後我就不服了,自己實現了一下,分享出來,反正也不難。 首選
是分組顯示,應ios那邊的要求。json字串做成了二維陣列的形式。java解析出來就是
List<Map<String,Object>>
這樣一個鬼東西。
分組的主要在於adapter. 這裡做了兩個item佈局,一個是group頭的,一個是每一列的。如下
group_item.xml
<LinearLayout xmlns:android ="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#fafcfd"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/reserveDateLayout"
android:layout_width="fill_parent"
android:layout_height="24dp"
android:background="@android:color/darker_gray">
<TextView
android:id="@+id/tvDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:text="123"
android:textColor="#000000"
android:textSize="20dp"
android:textStyle="bold" />
</RelativeLayout>
<LinearLayout
android:id="@+id/groupLayout"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
</LinearLayout>
</LinearLayout>
col_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="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/tvContent"
android:textSize="16dp"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="36dp"
android:text="123"/>
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:background="@android:color/darker_gray" />
</LinearLayout>
下面看看adapter
public class MyAdapter extends BaseAdapter {
private LayoutInflater inflater;
public MyAdapter(Context context) {
inflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder = null;
if (convertView == null) {
holder = new Holder();
convertView = inflater.inflate(R.layout.group_item, null);
holder.groupLayout = (LinearLayout) convertView.findViewById(R.id.groupLayout);
holder.tvDate = (TextView) convertView.findViewById(R.id.tvDate);
convertView.setTag(holder);
}else {
holder = (Holder) convertView.getTag();
holder.groupLayout.removeAllViews(); //一定要清空
}
Map<String, Object> listItem = list.get(position);
String date = listItem.get("date").toString();
holder.tvDate.setText(date);
List<String> itemList = (List<String>) listItem.get("col");
for (String item : itemList) {
View itemView = inflater.inflate(R.layout.col_item, null);
TextView tvContent = (TextView) itemView.findViewById(R.id.tvContent);
tvContent.setText(item);
tvContent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, ((TextView) v).getText().toString(), 0).show();
}
});
holder.groupLayout.addView(itemView);
}
return convertView;
}
}
public class Holder {
private LinearLayout groupLayout;
private TextView tvDate;
}
以上就是分組的實現 。這樣每次裡面item都得重新初始化,並沒有複用到。下面將實現頭標題的懸浮顯示。
1.改動activity佈局檔案,用FrameLayout把ListView包起來,並把頭的部分加起來
2. 初始化資料後 把頭顯示出來,並設定顯示的內容
3. 設定ListView的onScrollListener, 在onScroll方法中更新懸浮條
下面給出程式碼
actvity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context="mytest.jiang.wei.remoteview.MainActivity">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>
<RelativeLayout
android:visibility="gone"
android:id="@+id/floatGroupLayout"
android:layout_width="fill_parent"
android:layout_height="24dp"
android:background="@android:color/darker_gray">
<TextView
android:id="@+id/tvDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:text="123"
android:textColor="#000000"
android:textSize="20dp"
android:textStyle="bold" />
</RelativeLayout>
</FrameLayout>
</RelativeLayout>
下面是完整的Activity程式碼
public class MainActivity extends AppCompatActivity {
private ListView listView;
private RelativeLayout floatGroupLayout;
private List<Map<String, Object>> list;
private TextView tvDate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
floatGroupLayout = (RelativeLayout) findViewById(R.id.floatGroupLayout);
tvDate = (TextView) floatGroupLayout.findViewById(R.id.tvDate);
listView = (ListView) findViewById(R.id.listView);
initData();
listView.setAdapter(new MyAdapter(this));
listView.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if (totalItemCount > 0) {
tvDate.setText(list.get(firstVisibleItem).get("date").toString());
}
}
});
}
private void initData() {
list = new ArrayList<Map<String, Object>>();
for (int i = 0; i < 10; i ++) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("date", "2015-12-1" + i);
List<String> colList = new ArrayList<String>();
for (int j = 0; j < 10; j++) {
colList.add("第" + i + "組,第" + j + "條資料");
}
map.put("col", colList);
list.add(map);
}
//要放在初始後
floatGroupLayout.setVisibility(View.VISIBLE);
if (list.size() > 0) {
tvDate.setText(list.get(0).get("date").toString());
}
}
public class MyAdapter extends BaseAdapter {
private LayoutInflater inflater;
public MyAdapter(Context context) {
inflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder = null;
if (convertView == null) {
holder = new Holder();
convertView = inflater.inflate(R.layout.group_item, null);
holder.groupLayout = (LinearLayout) convertView.findViewById(R.id.groupLayout);
holder.tvDate = (TextView) convertView.findViewById(R.id.tvDate);
convertView.setTag(holder);
}else {
holder = (Holder) convertView.getTag();
holder.groupLayout.removeAllViews(); //一定要清空
}
Map<String, Object> listItem = list.get(position);
String date = listItem.get("date").toString();
holder.tvDate.setText(date);
List<String> itemList = (List<String>) listItem.get("col");
for (String item : itemList) {
View itemView = inflater.inflate(R.layout.col_item, null);
TextView tvContent = (TextView) itemView.findViewById(R.id.tvContent);
tvContent.setText(item);
tvContent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, ((TextView) v).getText().toString(), 0).show();
}
});
holder.groupLayout.addView(itemView);
}
return convertView;
}
}
public class Holder {
private LinearLayout groupLayout;
private TextView tvDate;
}
}