Android:在ListView项目中隐藏TextView时取消隐藏它

时间:2016-09-23 15:46:56

标签: java android listview

我对Android开发很新。所以我有一个包含LinearLayout的简单活动。这个LinearLayout包含几个按钮。每个按钮下方都隐藏着一个ListView。该按钮用于隐藏/取消隐藏相应的ListView。这些ListView中的每个项目都包含一个按钮和一个隐藏的TextView。这些子按钮用作隐藏/取消隐藏相应的TextView。在此之前,除了两件事以外,一切顺利。 a)当取消隐藏TextView时,我希望未隐藏的TextView占据整个地方,在我的情况下,它受ListView的高度限制。 b)使用其中一个子按钮隐藏/取消隐藏TextView时,按钮的重新排列顺序会发生变化。

一些图片和代码可以更好地理解它:

活动:

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_theory);
    LinearLayout ll = (LinearLayout) findViewById(R.id.linearlayout);
    //ll.setOrientation(LinearLayout.VERTICAL);
    titles = getResources().getStringArray(R.array.chapterTitleArray);

    for(int i = 0; i<titles.length; ++i){
        Button chapterButton = new Button(this);
        initChapter(chapterButton, titles[i], ll);
    }
}

private void initChapter(Button b, String chapterTitle, LinearLayout ll){
    rowItems.clear(); //For next chapter button
    //Adding Button
    b.setText(chapterTitle);
    ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    ll.addView(b,lp);

    //Adding listview
    final CustomListView subChaptersList = new CustomListView(this);
    lp = new ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

    ll.addView(subChaptersList, lp);

    subChapterTitles = getResources().getStringArray(R.array.subChapterTitleArray);
    descriptions = getResources().getStringArray(R.array.chapterTextArray);
    for(int i = 0; i<subChapterTitles.length; i++){
        RowItem item = new RowItem(subChapterTitles[i], descriptions[i]);
        rowItems.add(item);
    }
    CustomBaseAdapter adapter = new CustomBaseAdapter(this, rowItems);
    subChaptersList.setAdapter(adapter);
    subChaptersList.setBackgroundColor(Color.BLUE);
    subChaptersList.setVisibility(View.GONE);

    //Listener for Button
    b.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            subChaptersList.setVisibility((subChaptersList.getVisibility() == View.VISIBLE)
                ? View.GONE : View.VISIBLE);
        }
    });

适配器:

public class CustomBaseAdapter extends BaseAdapter{
Context c;
List<RowItem> rowItems;

public CustomBaseAdapter(Context c, List<RowItem> items){
    this.c = c;
    this.rowItems = items;
}

private class ViewHolder{
    final Button b;
    final TextView t;

    public ViewHolder(final View convertView, int pos){
        b = (Button) convertView.findViewById(R.id.button);
        t = (TextView) convertView.findViewById(R.id.text);
        RowItem rowItem = (RowItem) getItem(pos);
        b.setText(rowItem.getButtonText());
        t.setText(rowItem.getText());
        t.setVisibility(View.GONE);

        b.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                t.setVisibility((t.getVisibility() == View.VISIBLE)
                        ? View.GONE : View.VISIBLE);
            }
        });
    }
}

public View getView(int pos, View convertView, ViewGroup parent){
    ViewHolder holder = null;
    LayoutInflater mInflater = (LayoutInflater) c.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    if(convertView == null){
        convertView = mInflater.inflate(R.layout.list_item, parent, false);
        holder = new ViewHolder(convertView, pos);
        convertView.setTag(holder);
    }else{
        holder = (ViewHolder) convertView.getTag();
    }
    return convertView;
}

@Override
public int getCount(){
    return rowItems.size();
}

@Override
public Object getItem(int pos){
    return rowItems.get(pos);
}

@Override
public long getItemId(int pos){
    return rowItems.indexOf(getItem(pos));
}

我使用的自定义ListView根据内容更改ListView的高度:

public class CustomListView extends ListView {
public CustomListView  (Context context, AttributeSet attrs) {
    super(context, attrs);
}

public CustomListView  (Context context) {
    super(context);
}

public CustomListView  (Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int expandSpec = View.MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
            View.MeasureSpec.AT_MOST);
    super.onMeasure(widthMeasureSpec, expandSpec);
}

所以这是按下第一个按钮时的活动(由于自定义ListView,一切都很好):

activity when First button is pressed

这是按下第一个子按钮时的活动:

First subbutton pressed

请注意TextView如何取整个可用位置来显示整个文字。另外两个子按钮被切换(&#34; Point3&#34;用&#34; Point2&#34;切换)。再次隐藏/取消隐藏会再次切换。

我尝试了多种解决方案,我的第一种方法是使用LinearLayout持有ListView,每个项目持有Button / ListView,每个项目持有一个按钮/文本。我读到嵌入式ListView是不好的做法,所以我想出了解决方案几乎可以解决问题。我相信通过考虑ListView的高度来动态更改TextView的高度可能是一个解决方案,但我不知道该修改的位置。

0 个答案:

没有答案