2016-09-23 60 views
1

我有一個自定義的listview包含一些。餘設置的列表視圖的adapter如下:如何使listview的textview可點擊

BestandTypAdapter bestandTypAdapter = new BestandTypAdapter(getActivity(), R.layout.bestand_type_liste, dataList); 
li.setAdapter(bestandTypAdapter); 

並且如bestand_type_liste佈局的代碼顯示在下面的TextView的是可點擊

bestand_type_liste

<?xml version="1.0" encoding="utf-8"?> 
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:orientation="horizontal" 
android:id="@+id/layout_depots_typ" 
android:padding="5dip" > 

<TableRow 
    android:paddingRight="10dip" 
    android:paddingLeft="10dip" 
    > 

    <TextView 
     android:id="@+id/BESTAND_TYP_NAME" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_gravity="left" 
     android:layout_weight="0.50" 
     android:clickable="true"/> <<================= 

getView()方法adapter我正在使用onClickListener與名稱TexView相關聯,這樣當名稱被點擊時位置如下的logCat顯示:

holder.name.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Log.i("", "position_ : " + pos); 
      } 
     }); 

的問題是,當我在listview項目沒有點擊出現,如何解決這個問題

+0

你並不需要設置'機器人:點擊= 「true」到'textView'。 –

+0

只是一個問題:爲什麼「holder.name.set ...」而不是「myTextView.set ...」? –

回答

1

只是嘗試添加該屬性爲TextView的:

android:focusable="false" 
+0

實際上問題仍然存在 – user2121

+0

請提供關於你的代碼的更多細節:) – alway5dotcom

0

陣列適配器的示例,這是主要活動。

public class ListViewMain extends AppCompatActivity { 
private ArrayList<BrandModel> alBrand; 
private ListView list; 
private CustomAdapter custAdapter; 
private AdapterView.OnItemClickListener messageClickedHandler; 
private View vHeader; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    init(); 
    setupDefaults(); 
    setupEvents(); 
} 

public void init() { 
    //list view 
    list = (ListView) findViewById(R.id.listView); 
    //Array list 
    alBrand = new ArrayList<>(); 
    //Custom Adapter 
    custAdapter = new CustomAdapter(this, R.layout.textlistview, alBrand); 
    //View 
    vHeader = getLayoutInflater().inflate(R.layout.listviewheader, null, false); 
} 

public void setupDefaults() { 
    addBrandAndAdapter(); 
} 

public void addBrandAndAdapter() { 
    alBrand.add(new BrandModel(getResources().getString(R.string.And), R.drawable.android)); 
    alBrand.add(new BrandModel(getResources().getString(R.string.Mac), R.drawable.apple)); 
    alBrand.add(new BrandModel(getResources().getString(R.string.Tizen), R.drawable.insta)); 
    alBrand.add(new BrandModel(getResources().getString(R.string.window), R.drawable.windows)); 
    list.addHeaderView(vHeader); 
    list.setAdapter(custAdapter); 
} 

public void setupEvents() { 
    messageClickedHandler = new AdapterView.OnItemClickListener() { 
     public void onItemClick(AdapterView parent, View view, int position, long id) { 
      Toast.makeText(getApplicationContext(), "Click ListItem Number " + position, Toast.LENGTH_LONG).show(); 
      System.out.println("Selected"); 
     } 
    }; 
    list.setOnItemClickListener(messageClickedHandler); 
} 

}

  • Arrayadapter

  • public class CustomAdapter extends ArrayAdapter<BrandModel> { 
    ArrayList<BrandModel> alBrand = new ArrayList<>(); 
    
    public CustomAdapter(Context context, int tvResId, ArrayList<BrandModel> alObjects) { 
        super(context, tvResId, alObjects); 
        alBrand = alObjects; 
    } 
    
    @Override 
    public int getCount() { 
        return super.getCount(); 
    } 
    
    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
        View add = convertView; 
        BrandHolder holder = null; 
        if (add == null) { 
         LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
         add = inflater.inflate(R.layout.textlistview, null); 
         holder = new BrandHolder(); 
         holder.tvName = (TextView) add.findViewById(R.id.tvName); 
         holder.ivImage = (ImageView) add.findViewById(R.id.ivIcon); 
         add.setTag(holder); 
    
        } else { 
         holder = (BrandHolder) add.getTag(); 
        } 
        holder.tvName.setText(alBrand.get(position).getStrBrdName()); 
        holder.ivImage.setImageResource(alBrand.get(position).getImgBrdLogo()); 
        return add; 
    } 
    
    static class BrandHolder { 
        TextView tvName; 
        ImageView ivImage; 
    } 
    

    }

    +0

    好吧,但我想從裏面做getView() – user2121

    +0

    不要把你的getView方法@ user2121,或請把完整的代碼 –

    +0

    或嘗試OnItemClickListener在getView()方法@ user2121 –

    相關問題