【问题标题】:using ListView in swipe pages在滑动页面中使用 ListView
【发布时间】:2013-06-25 19:36:52
【问题描述】:

您好,我有一个包含 3 页的滑动页面示例,我想在此示例中添加一个列表视图。
但是当我尝试运行那个 Eclipse 时说“你的内容必须有一个 id 属性为 'android.R.id.list' 的 ListView”。我不知道怎么了。
我的主要活动布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <android.support.v4.view.ViewPager
        android:id="@+id/customviewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />    

     </LinearLayout>    

第1页

<?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:background="@color/blue"
android:orientation="vertical" >     

<Button
    android:id="@+id/buttonBlue"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/label_blue"
    android:onClick="@string/listenerBlueButton" />

 </LinearLayout>     

第2页

    <?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:background="@color/yellow"
    android:orientation="vertical" >

    <Button
        android:id="@+id/buttonYellow"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/label_yellow"
        android:onClick="@string/listenerYellowButton" />

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

    </LinearLayout>  

第 3 页

<?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:background="@color/red"
android:orientation="vertical" >

<Button
    android:id="@+id/buttonRed"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/label_red"
    android:onClick="@string/listenerRedButton" />

</LinearLayout>

我的寻呼机适配器

   package ro.ovidiuconeac.horizontalviewswiping;

     public class CustomPagerAdapter extends PagerAdapter {

    public Object instantiateItem(View collection, int position) {

        LayoutInflater inflater = (LayoutInflater) collection.getContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        int resId = 0;
        switch (position) {
        case 0: {
            resId = R.layout.page_1;
            break;
        }
        case 1: {
            resId = R.layout.page_2;
            break;
        }
        case 2: {
            resId = R.layout.page_3;
            break;
        }
        }

        View view = inflater.inflate(resId, null);

        ((ViewPager) collection).addView(view, 0);

        return view;
    }

    @Override
    public void destroyItem(View arg0, int arg1, Object arg2) {
        ((ViewPager) arg0).removeView((View) arg2);
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == ((View) arg1);

    }

    @Override
    public Parcelable saveState() {
        return null;
    }

    @Override
    public int getCount() {
        return 3;
    }
    }

主要活动

public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Create and set adapter
    CustomPagerAdapter adapter = new CustomPagerAdapter();
    ViewPager myPager = (ViewPager) findViewById(R.id.customviewpager);
    myPager.setAdapter(adapter);
    myPager.setCurrentItem(1);

    String[] ex=new String[10];
    ex[0]="1";
    ex[1]="2";
    ex[2]="3";
    ex[3]="1";
    ex[4]="2";
    ex[5]="3";
    ex[6]="1";
    ex[7]="2";
    ex[8]="3";
    ex[9]="3";

    setContentView(R.layout.page_2);
    ListView myList=(ListView)findViewById(android.R.id.list);
    ListAdapter la=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, ex);

    myList.setAdapter(la);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

/**
 * Click button on blue page
 */
public void onClickBlueButton(View v) {
    Toast.makeText(getApplicationContext(), "Blue screen", Toast.LENGTH_SHORT).show();
}

/**
 * Click button on yellow page
 */
public void onClickYellowButton(View v) {
    Toast.makeText(getApplicationContext(), "Yellow screen", Toast.LENGTH_SHORT).show();
}

/**
 * Click button on red page
 */
public void onClickRedButton(View v) {
    Toast.makeText(getApplicationContext(), "Red screen", Toast.LENGTH_SHORT).show();
}
}

【问题讨论】:

  • 您的 ListActivity 正在膨胀您的主要活动的布局,而不是您的页面 2(包含您的列表视图)的布局。请查看我的更新答案,如果它有效,请告诉我
  • @ggmax 这能解决你的问题吗??

标签: android eclipse listview swipe swipeview


【解决方案1】:

这是因为您正在扩展 ListActivity。 ListActivity 从 android 包中查找具有特定标识符的 ListView。

看看你的 ListActivity 的这一行

setContentView(R.layout.activity_main);

您的 ListActivity 正在膨胀您的主要活动的布局,而不是您的页面 2(包含您的列表视图)的布局。

您需要在 ListActivity 的 onCreate 中更改该行以填充正确的布局。假设您的 ListActivity 布局称为 R.layout.list_activy。那条线就变成了

setContentView(R.layout.list_activity);

而不是你目前拥有的,我不确定你的包含列表视图的布局文件的名称是什么,你没有提到它(页面?)。

编辑:我只记得 ListActivity 可能根本不需要调用 setContentView(),因为无论如何您都在 setAdapter() 中将父布局传递给您的适配器。尝试完全删除该行,然后调用 setAdapter。不过,这两种方法都应该有效

【讨论】:

  • 您需要更改 ListActivity 中的 setContentView() 行以填充正确的布局,即包含您的列表视图的布局。请查看我的更新答案
  • 是的,我看到了,但问题是当我将 setContentView() 从 (R.layout.activity_main) 更改为 (R.layout.list_activity) 时,我的滑动寻呼机不起作用。再次感谢您。
  • R.layout.list_activity 是包含您的 ListView 的布局文件的实际名称吗?尝试完全删除对 setContentView() 的调用。 ListActivity 仍会寻找您传递给适配器的布局
  • 不,它是 R.layout.page_2 ,我现在将 Listactivity 更改为活动它的工作,但我的寻呼机滑动不起作用。请查看我编辑的活动。
  • 如果您将 ListActivity 更改为 Activity,那么您必须将 listview 的 ID 而不是 android.R.id.list 更改为其他内容
【解决方案2】:

您的 XML 中有 ListView,但您没有将适配器传递给它。创建一个ListView 并将您的适配器传递到此列表视图。

试试这个

ListView myList=(ListView)findViewById(android.R.id.list);
myList.setAdapter(la);

【讨论】:

  • 我尝试过,但仍然无法正常工作。 ListAdapter la=new ArrayAdapter(this, android.R.layout.simple_list_item_1, ex); ListView myList=(ListView)findViewById(android.R.id.list); myList.setAdapter(la);错误您的内容必须有一个 id 属性为 'android.R.id.list' 的 ListView
猜你喜欢
  • 2015-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多