2013-10-11 24 views
0

我有一個ListView,其中行佈局有TextView兩行限制:如何在用戶單擊時在ListView中顯示更多行?

android:lines="2" 

所以,在默認情況下每個ListView的行顯示如下:

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 

我想顯示/隱藏所有用戶單擊行時用於特定行的行。比方說,用戶點擊第1行,我想展示一些東西,如:

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 

我在想ExpandableListView使用,但是似乎它的目的是不同的。

我該如何實現我在找的東西?

+0

它可能只是我,但您可能需要發佈更多關於這裏究竟發生了什麼的信息,我不太關注。 – gh123man

+0

@ gh123man,我已經添加了示例 –

+1

爲什麼你不能在項目點擊時調用'textView.setLines(4)'? – Desert

回答

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"); 
    }  
} 
1

我假設你有一個自定義列表適配器和列表視圖項目佈局。 將所有線條包裹在一些佈局中。 給這個佈局一個ID。 在您的代碼中引用該ID併爲其創建一個視圖對象。

實現您的列表適配器的方法,它的確是這樣的:

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

附加一個onItemClickListener列表視圖。

做一些事情的效果 - 當用戶點擊一個項目時,調用你的列表適配器中的一個函數來切換可見性。

您可以進一步擴展該方法以在其位置放置一些默認視圖或使其他視圖可見。

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"); 
     } 
    } 
+0

謝謝。看起來像點擊我應該改變這個值已經打開的行。而且,採用這種方法我遇到了以下問題 - 如果我點擊然後滾動,那麼隨機行就會被打開。 –

+0

@LA_我編輯Yoann的回答,一旦他批准它,你會看到我解決這個問題的方法。 – bclymer

+0

bclymer其實我無法批准它,它一開始就被拒絕了。無論如何,我看到你的想法,這是我在我的應用程序中做的,我會更新我的答案以顯示它。 –

相關問題