我正在使用带有可滑动示例的选项卡布局,选项卡上的列表是列表视图,当单击列表项时,将打开新片段。我的问题是,当我点击列表项时,没有任何事情发生。 我正在使用以下示例并添加我的代码androidhive.info/category/tab-view
AddingList
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.ListFragment;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class AddingList extends ListFragment {
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
String[] values = new String[] { "List", "iPhone", "WindowsMobile",
"Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
"Linux", "OS/2" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.pager, new List()).commit();
}
}
答案 0 :(得分:3)
从onActivityCreated()方法
移动onCreateView()方法中的代码 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
String[] values = new String[] { "List", "iPhone", "WindowsMobile",
"Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
"Linux", "OS/2" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
return super.onCreateView(inflater, container, savedInstanceState);
}
的更新:强> 的
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
FragmentManager manager = getFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
ft.replace(R.id.pager, new Home());
ft.commit();
}
在Fragment中实现此方法。
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
// Make sure that we are currently visible
if (this.isVisible()) {
// Do your stuff here
if (!isVisibleToUser) {
Log.d("MyFragment", "Not visible");
}
}
}
答案 1 :(得分:1)
试试这个
ListView list = (ListView) findViewById(R.id.pager1);
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View view, int arg2,
long arg3) {
// TODO Auto-generated method stub
//Your action
}
});
答案 2 :(得分:1)
试试这个。需要设置适配器
ListView mListContainer = (ListView) findViewById(R.id.pager1);
//YOURADAPTER NAME
mInformedListAdapter = new InformedListAdapter(mContext, mStatistics,this);
//mStatistics is the my private Map<String, Map<String, String>> mStatistics = new HashMap<String, Map<String, String>>();
mListContainer.setAdapter(mInformedListAdapter);
mListContainer.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//DO YOURS
}
});
答案 3 :(得分:0)
你们必须在覆盖方法
中调用super.onItemClickListener方法