用例如下: 我想在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的监听器:
main.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>
submain.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;
}
问题是我失去了听众,没有触发。 我发现的解决方案减缓了我的应用程序。
我将侦听器放在适配器内,用于每个项目。
答案 0 :(得分:0)
您的ViewPager设置为高度0px
。给它一个更大的高度,你应该能够看到它。也许:
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" >
修改强>
显示您要创建此片段的代码并将其添加到屏幕。还要为此构造函数添加一些日志记录:
public HeaderFragment(List<Message> messages, String topic) {
this.messages = messages;
Log.i("TAG", "messages size: " + this.messages.size(); // <- add this
this.topic = topic;
}
确保messages
在传递到此处时包含一些项目。此片段从不向messages
添加任何项目,因此如果在创建片段时它没有任何项目,则ListView中将不会显示任何项目。