0

I have a ListView, where the row layout has TextView with two lines limit:

android:lines="2"

So, by default each ListView row displayed like:

  this is line 1 of the row 1
  this is line 2 of the row 1
  ---------------------------
  this is line 1 of the row 2
  this is line 2 of the row 2

I would like to show/hide all lines for the particular row, when user clicks on the row. Let's say user clicked on the row 1, I would like to show something like:

  this is line 1 of the row 1
  this is line 2 of the row 1
  this is line 3 of the row 1
  this is line 4 of the row 1
  ---------------------------
  this is line 1 of the row 2
  this is line 2 of the row 2

I was thinking about ExpandableListView usage, but seems its purpose is different.

How can I implement what I am looking for?

4

3 回答 3

1

我假设您有一个自定义列表适配器和一个列表视图项布局。将所有线条包裹在某种布局中。为该布局指定一个 ID。在您的代码中引用 ID 并为其创建一个视图对象。

实现您的列表适配器的方法,该方法执行以下操作:

public void toggleVisible() {
    if (viewContainingLines.getVisibility() == View.VISIBLE)
        viewContainingLines.setVisibility(View.INVISIBLE);
    else 
        viewContainingLines.setVisibility(View.VISIBLE);
}

将 onItemClickListener 附加到列表视图。

并做一些事情——当用户点击一个项目时,在你的列表适配器中调用一个函数来切换可见性。

您可以进一步扩展该方法以将一些默认视图放置在其位置,或使其他内容可见。

于 2013-10-11T14:53:53.500 回答
1

您只需要动态更改TextView您在行中使用的最大行数,例如:

textView.setMaxLines(Integer.MAX_VALUE);

这是我创建的每次单击一行时执行的函数:

private void rowItemExpand(){
        try {
            //Get back the last textview touched 
            TextView textView = lastTouchedTextView;

            boolean fullText;
            if(textView.getTag() == null){
                fullText = false;
            } 
            else {
                fullText = (Boolean) textView.getTag();
            }

            if(fullText){
                textView.setMaxLines(mPrefs.getListViewMaxLines());
                //Keep a flag to show the textview is opened
                textView.setTag(false);
            }
            else{
                textView.setMaxLines(Integer.MAX_VALUE);
                textView.setTag(true);
            }
        }
        catch(Exception e) {
            Log.e(TAG,"rowItemExpand - Cannot get the selected index");
        }
    }
于 2013-10-11T14:56:33.630 回答
0

最后我使用了以下代码(只允许打开一项):

private int openedItem;

...

@Override
protected void onCreate(Bundle savedInstanceState) {
    openedItem = -1;

...

adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {

    @Override
    public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
        int viewId = view.getId();
        switch(viewId) {
        case R.id.sms_body:
            // override this to be able to handle open/closed listview items
            // see also onListItemClick below
            TextView textView = (TextView) view;
            textView.setText(cursor.getString(columnIndex));

            if (cursor.getPosition() == openedItem) {
                textView.setMaxLines(Integer.MAX_VALUE);
            } else {
                textView.setMaxLines(2);
            }   
            return true;


@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);

    try {
        TextView textView = (TextView) v.findViewById(R.id.sms_body);            
        if (openedItem == position) {
            textView.setMaxLines(2);
            openedItem = -1;
        } else {
            textView.setMaxLines(Integer.MAX_VALUE);
            openedItem = position;
            adapter.notifyDataSetChanged(); // call this to close all other opened rows
        }      
    } catch (Exception e) {
        Log.e(TAG, "Cannot get the selected index");
    }       
}
于 2013-10-12T15:44:00.010 回答