具有可扩展布局的ListView

时间:2016-08-05 19:44:15

标签: java android listview android-studio

我有ListView,其中有数百item

我希望当用户点击某个项目(onClick)时,它会展开并显示一些有关它的详细信息。

但是我希望展示布局,例如RelativeLayoutLinearLayout

我想我必须制作数百种布局。也许有更好的方法?

因为每个项目的不同描述(价格,国家等)。

我已经知道ExpandbleListView

我不想使用child.add("Android");,因为我希望扩展视图像桌面视图一样进行组织。

因此,当我点击它时,它会展开并显示布局。

我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:0)

LinearLayoutRelativeLayout构建为列表项视图的一部分,并最初将visibility设置为false

现在跟踪全局数组中展开的项目,以便在列表中设置扩展视图的正确可见性。

我正在分享一些可能有助于您理解的伪代码。

// Take a global array to keep track of the expanded items in your list
// Lets assume the name of your list is myArrayList
private void int[] expandedItems = new int[myArrayList.size()];

// Now initialize your array with all zeros 
for(int i=0; i<expandedItem.length; i++) 
    expandedItems[i] = 0;

现在,当点击某个项目时,请更新expandedItems数组中的相应位置。

itemView.setOnClickListener {
    //....
    @Override
    public void onClick(View v){
        // Set 1 to indicate the corresponding is view is expanded
        expandedItems[position] = 1;
        notifyDatasetChanged();
    }
}

现在在onBindViewHolder填写列表如下

if(expandedItems[position] == 1)
    expandedLinearLayout.setVisibility(View.VISIBLE);
else
    expandedLinearLayout.setVisibility(View.GONE);  // Don't forget the else part
相关问题