0

在我的主要活動中,我有一個包含導航選項卡和操作項的ActionBar。我的一個操作項調用一個ListFragment,它顯示一個帶有圖像和文本視圖的自定義列表視圖。我使用ActionBarSherlock來兼容。這裏就是我所說的新ListFragment代碼:onListItemClick不在ListActivity中觸發

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
    case android.R.id.home: 
     Intent intent = new Intent(this, ERGProActivity.class); 
     intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     startActivity(intent); 
     return true; 
    case R.id.menuitem_search: 
     onSearchRequested(); 
     return true; 
    case R.id.menuitem_info: 
     // Create new fragment and transaction 
     SherlockListFragment aboutListFragment = new AboutListFragment(); 
     FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 

     // Replace whatever is in the fragment_container view with this fragment, 
     // and add the transaction to the back stack 
     ft.replace(R.id.root, aboutListFragment); 
     ft.addToBackStack(null); 
     ActionBar actionBar = getSupportActionBar(); 
     actionBar.setDisplayHomeAsUpEnabled(true); 

     // Commit the transaction 
     ft.commit(); 
     return true; 

    default: 
     return super.onOptionsItemSelected(item); 
    } 
} 

這裏是我的ListFragment代碼:

public class AboutListFragment extends SherlockListFragment { 

    private ListView listView; 

    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 
     getActivity().setContentView(R.layout.aboutlist); 

     CustomMenu menu_data[] = new CustomMenu[] 
       { new CustomMenu(R.drawable.info, "About"), 
        new CustomMenu(R.drawable.legal, "Disclaimer"),}; 

       CustomMenuAdapter adapter = new CustomMenuAdapter(getActivity(), 
         R.layout.listview_item_row, menu_data); 
       listView = (ListView) getActivity().findViewById(R.id.listView1); 
       listView.setAdapter(adapter); 
    } 

    @Override 
    public void onListItemClick(ListView l, View v, int position, long id) { 
     switch (position) { 
     case 0: 
      // about 
      Log.i("ABOUT"," - About Selected"); 
     case 1: 
      Log.i("ABOUT"," - Disclaimer Selected"); 
      // disclaimer 
     } 
    } 
} 

這裏是我的CustomAdapter代碼:

public class CustomMenuAdapter extends ArrayAdapter<CustomMenu> { 
    Context context; 
    int layoutResourceId;  
    CustomMenu data[] = null; 

    public CustomMenuAdapter(Context context, int layoutResourceId, CustomMenu[] data) { 
     super(context, layoutResourceId, data); 
     this.layoutResourceId = layoutResourceId; 
     this.context = context; 
     this.data = data; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View row = convertView; 
     CustomMenuHolder holder = null; 

     if(row == null) 
     { 
      LayoutInflater inflater = ((Activity)context).getLayoutInflater(); 
      row = inflater.inflate(layoutResourceId, parent, false); 

      holder = new CustomMenuHolder(); 
      holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon); 
      holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle); 

      row.setTag(holder); 
     } 
     else 
     { 
      holder = (CustomMenuHolder)row.getTag(); 
     } 

     CustomMenu menu = data[position]; 
     holder.txtTitle.setText(menu.title); 
     holder.imgIcon.setImageResource(menu.icon); 

     return row; 
    } 

    static class CustomMenuHolder 
    { 
     ImageView imgIcon; 
     TextView txtTitle; 
    } 
} 

任何輸入或建議將是不勝感激!先謝謝了。

謝謝大家的意見!這裏是最後的工作代碼:

@Override 
public void onActivityCreated(Bundle savedInstanceState) { 
    super.onActivityCreated(savedInstanceState); 
    getActivity().setContentView(R.layout.aboutlist); 

    CustomMenu menu_data[] = new CustomMenu[] 
      { new CustomMenu(R.drawable.info, "About"), 
       new CustomMenu(R.drawable.legal, "Disclaimer"),}; 

      CustomMenuAdapter adapter = new CustomMenuAdapter(getActivity(), 
        R.layout.listview_item_row, menu_data); 
      listView = (ListView) getActivity().findViewById(R.id.listView1); 
      // listView.setOnItemSelectedListener(this); 
      listView.setAdapter(adapter); 
      listView.setOnItemClickListener(new OnItemClickListener() { 

       public void onItemClick(AdapterView<?> parent, View view, 
         int position, long id) { 

        switch (position) { 
          case 0: 
           Log.i("ABOUT"," - About Selected"); 
           break; 
          case 1: 
           Log.i("ABOUT"," - Disclaimer Selected"); 
           break; 
          default: 
           // do nothing 
           Log.i("ABOUT"," - default"); 
           break; 
           } 
        } 
      }); 
    } 
+0

集 「可聚焦:假」 TextView的和ImageView的的。 我認爲這可能有效。 –

+0

你的列表視圖中只有兩個項目嗎?現在,如果您點擊列表中前兩項的任何內容,由於您的switch語句,將不會發生任何事情。 你是否真的試圖從textview或圖像獲取點擊? – Barak

+0

我試圖從listView中獲取點擊。我能夠得到它的工作,我也修復了我的開關/案例聲明。 – wyoskibum

回答

1
listView.setOnItemClickListener(new OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 
        long arg3) { 
       // TODO Auto-generated method stub 

      } 
     }); 

嘗試使用此代碼。

+0

感謝您的回覆。我能夠使用onItemClick的變體來使其工作。我將發佈任何其他人與相似情況的工作代碼。 – wyoskibum

0

使用下面的代碼:

listView.setOnItemSelectedListener(this); 

實施方法後,你會得到如下方法:

@Override 
    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, 
      long arg3) { 
     // TODO Auto-generated method stub 
Do your code here..... 

    } 

    @Override 
    public void onNothingSelected(AdapterView<?> arg0) { 
     // TODO Auto-generated method stub 

    } 
+0

感謝您的回覆!我能夠使用yogesh kamaliya的建議來工作。 – wyoskibum

相關問題