您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
安卓listview實現加載獨立的xml佈局及listview的嵌套
发布时间:2021-06-24 21:35:33编辑:雪饮阅读()
在前面咱們實現了listview,用以在視頻選取的時候展示一個視頻列表。當時是通過代碼實現的,如:
public int getCount() {
return videolist.size();
}
public Object getItem(int position) {
return videolist.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
VideoInfo videoInfo = videolist.get(position);
TextView tv = new TextView(VideoSelect.this);
tv.setText(videoInfo.getDisplayName());
ImageView imageView = new ImageView(VideoSelect.this);
loadCover(imageView,videoInfo.getAbsolutePath(), VideoSelect.this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(400,400);
imageView.setLayoutParams(params);
LinearLayout lv = new LinearLayout(VideoSelect.this);
lv.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lv.setLayoutParams(lp);//设置布局参数
lv.addView(imageView);
lv.addView(tv);
return lv;
}
}
這裏最重要的還就是這個LayoutInflater,即佈局增壓泵。 //解决嵌套listview时,子listview的item只显示一个的问题 public View getView(int position, View convertView, ViewGroup parent) {
public int getCount() {
return videoDirList.size();
}
public Object getItem(int position) {
return videoDirList.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
VideoInfo videoDir = videoDirList.get(position);
LayoutInflater inflater = LayoutInflater.from(ActivityCutLibrary.this);
View view = inflater.inflate(R.layout.other_listview_item, null);
TextView cut_from_name=view.findViewById(R.id.cut_from_name);
cut_from_name.setText(videoDir.getDisplayName());
File child_scan_dir=new File(scan_dir.toString()+File.separator+videoDir.getDisplayName());
ListView videoListView = view.findViewById(R.id.videolist);
VideoAdatper VA=new VideoAdatper(child_scan_dir);
videoListView.setAdapter(VA);
setListViewHeightBasedOnChildren(videoListView);
view.setTag("dir"+position);
return view;
}
}
dirListView = (ListView) this.findViewById(R.id.videoDirList);
DirAdatper DA=new DirAdatper();
dirListView.setAdapter(DA);
public int getCount() {
return videoDirList.size();
}
public Object getItem(int position) {
return videoDirList.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
VideoInfo videoDir = videoDirList.get(position);
LayoutInflater inflater = LayoutInflater.from(ActivityCutLibrary.this);
View view = inflater.inflate(R.layout.other_listview_item, null);
TextView cut_from_name=view.findViewById(R.id.cut_from_name);
cut_from_name.setText(videoDir.getDisplayName());
File child_scan_dir=new File(scan_dir.toString()+File.separator+videoDir.getDisplayName());
ListView videoListView = view.findViewById(R.id.videolist);
VideoAdatper VA=new VideoAdatper(child_scan_dir);
videoListView.setAdapter(VA);
setListViewHeightBasedOnChildren(videoListView);
view.setTag("dir"+position);
return view;
}
}
VideoAdatper VA=new VideoAdatper(child_scan_dir);
videoListView.setAdapter(VA);
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
return;
}
int totalHeight = 0;
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
}該方法只對以LinearLayout實現的佈局有用,是因爲只有LinearLayout佈局中有measure方法的實現。 最後補充下,這個雙層嵌套listview並不是很好,因爲就目前而言,大家都是這樣認爲的,并且就我而言目前發現在二層listview中的item中無法獲得當前listview的上級listview的position,那麽在做某些要響應上級的功能的時候就會很棘手,雖然有邪道的用法,但是筆記寫道,咱們是正派人士。
要做事就該脚踏實地不是嗎?
關於二級列表這種,經過網友瞭解到好像有個叫ExpandableListView的可以實現。我明天去試試,今晚不一定有時間了。
另外追加分享一個小技巧,你可以看到我這裏用OnClickListener是在getView中實現的,而不是統一在適配器中實現的,這樣以來對於比如你每個行中有多個按鈕時候,而不是這一行整體就一個反饋事件來説,就可以很好的區分該行中具體哪一個按鈕的事件了,因爲這樣就可以為每個按鈕單獨定義事件。
VideoInfo videoInfo = videolist.get(position);
LayoutInflater inflater = LayoutInflater.from(ActivityCutLibrary.this);
View view = inflater.inflate(R.layout.other_listview_item_item, null);
ImageView iv=view.findViewById(R.id.iv);
loadCover(iv,videoInfo.getAbsolutePath(), ActivityCutLibrary.this);
Button delete_bt=view.findViewById(R.id.delete_bt);
VideoAdatper that=this;
View.OnClickListener listener=new View.OnClickListener(){
@Override
public void onClick(View v)
{
if(v==delete_bt){
Message msg = new Message();
String absolutePath=videoInfo.getAbsolutePath();
msg.obj=new VideoDeleteHandler(absolutePath,videolist,that,ActivityCutLibrary.this,position);
videoDeleteHandler.sendMessage(msg);
}
}
};
delete_bt.setOnClickListener(listener);
TextView tv = (TextView) view.findViewById(R.id.tv);
tv.setText(videoInfo.getDisplayName());
return view;
}
关键字词:listview,listview嵌套