【问题标题】:ListView with expandable layout具有可扩展布局的 ListView
【发布时间】:2016-08-05 19:44:14
【问题描述】:

我有一个ListView,其中包含数百个item

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

但是我想展示一个布局,比如RelativeLayoutLinearLayout

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

因为每件商品的描述不同(价格、国家等)。

我已经知道ExpandbleListView

我不想使用child.add("Android");,因为我希望展开的视图像表格视图一样组织。

所以当我点击它时,它会展开并显示一个布局。

我怎样才能做到这一点?

【问题讨论】:

标签: java android listview android-studio


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2011-10-27
    • 2014-01-06
    • 2018-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多