用例如下:我想在 ViewPager 中加载一个 ListView,同时在列表上设置一个 OnItemClickListener。
有人可以告诉为什么 ListView 没有加载,即屏幕保持黑色。甚至传呼机的条带也不起作用。
布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:padding="4dip" >
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="0px" >
<android.support.v4.view.PagerTitleStrip
android:id="@+id/pager_title_strip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:layout_weight="1"
android:background="#33b5e5"
android:paddingBottom="4dp"
android:paddingTop="4dp"
android:textColor="#fff" />
<ListView
android:id="@+id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:drawSelectorOnTop="false" />
</android.support.v4.view.ViewPager>
一些代码:
public static class HeaderFragment extends ListFragment {
List<Message> messages = new ArrayList<Message>();
String topic;
public HeaderFragment(List<Message> messages, String topic) {
this.messages = messages;
this.topic = topic;
}
public HeaderFragment() {
// TODO Auto-generated constructor stub
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.main, container, false);
final ListView listView = (ListView) v.findViewById(android.R.id.list);
final MessageItemAdapter adapter = new MessageItemAdapter(inflater,
R.layout.headers, messages, 1);
//the click listener.
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Message message = adapter.getItem(position);
Intent intent = new Intent();
intent.setClassName(
"main",
"NewsActivity");
intent.putExtra("title", message.getTitle());
intent.putExtra("description", message.getDescription());
intent.putExtra("source", message.getLink().toString());
intent.putExtra("language", location);
startActivity(intent);
}
});
listView.setAdapter(adapter);
return v;
}
提前致谢 !!!
编辑:
调用片段的代码:
public class HeadersActivityPagerAdapter extends FragmentStatePagerAdapter {
public HeadersActivityPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {
int k = 0;
String topic = null;
List<Message> messages = new ArrayList<Message>();
for (Entry<String, List<Message>> elem : fragmentsData.entrySet()) {
if (k==i)
{
topic = elem.getKey();
messages = elem.getValue();
break;
}
k++;
}
return new HeaderFragment(messages, topic);
}
@Override
public int getCount() {
return fragmentsData.size();
}
@Override
public CharSequence getPageTitle(int position) {
int k = 0;
String topic = "";
for (Entry<String, List<Message>> elem : fragmentsData.entrySet()) {
if (k==position)
{
topic = elem.getKey();
break;
}
k++;
}
return topic;
}
}
它像这样工作,但我失去了 ListView 的侦听器:
主要的.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:padding="4dip" >
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1" >
<android.support.v4.view.PagerTitleStrip
android:id="@+id/pager_title_strip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="#33b5e5"
android:paddingBottom="4dp"
android:paddingTop="4dp"
android:textColor="#fff" />
</android.support.v4.view.ViewPager>
子主.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" >
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:drawSelectorOnTop="false" />
</FrameLayout>
一些代码:
public static class HeaderFragment extends ListFragment {
List<Message> messages = new ArrayList<Message>();
String topic;
public HeaderFragment(List<Message> messages, String topic) {
this.messages = messages;
this.topic = topic;
}
public HeaderFragment() {
// TODO Auto-generated constructor stub
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.submain, container, false);
final ListView listView = (ListView) v.findViewById(android.R.id.list);
final MessageItemAdapter adapter = new MessageItemAdapter(inflater,
R.layout.headers, messages, 1);
//the click listener.
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Message message = adapter.getItem(position);
Intent intent = new Intent();
intent.setClassName(
"main",
"NewsActivity");
intent.putExtra("title", message.getTitle());
intent.putExtra("description", message.getDescription());
intent.putExtra("source", message.getLink().toString());
intent.putExtra("language", location);
startActivity(intent);
}
});
listView.setAdapter(adapter);
return v;
}
问题是我失去了我的听众,没有触发。我找到的解决方案减慢了我的应用程序。
对于每个项目,我将侦听器放置在适配器内。