Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android ListView行项目中的ViewPager_Android_Android Listview_Android Viewpager - Fatal编程技术网

Android ListView行项目中的ViewPager

Android ListView行项目中的ViewPager,android,android-listview,android-viewpager,Android,Android Listview,Android Viewpager,我已经阅读了很多关于这个问题的相关问题。然而,这些问题都没有得到答复。我正在尝试向列表视图中的每一行添加一个查看页面,其中页面中有两个布局。我相信这是可行的,因为 现在我已经实现了它。但是,在中间显示一行空白页。没有出现任何行 这是我的密码: 主活动+阵列适配器 public class MainActivity extends Activity { ListView list; String[] heros; @Override protected voi

我已经阅读了很多关于这个问题的相关问题。然而,这些问题都没有得到答复。我正在尝试向
列表视图
中的每一行添加一个
查看页面
,其中页面中有两个布局。我相信这是可行的,因为

现在我已经实现了它。但是,在中间显示一行空白页。没有出现任何行

这是我的密码:

主活动+阵列适配器

public class MainActivity extends Activity {

    ListView list;
    String[] heros;

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

        heros = getResources().getStringArray(R.array.heros);

        list = (ListView) findViewById(R.id.listView1);
        list.setAdapter(new CustomAdapter(this, heros));
    }

    public class CustomAdapter extends ArrayAdapter<String>
    {
        String[] heros;
        Context context;
        public CustomAdapter(Context context, String[] heros) {
            super(context, R.layout.pager_item_list, R.id.listView1, heros);
            // TODO Auto-generated constructor stub
            this.heros = heros;
            this.context = context;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View row = inflater.inflate(R.layout.pager_item_list, parent, false);

            Log.d("how many times", "I really don't know how it should be");

            ViewPager pager = (ViewPager) row.findViewById(R.id.pager);
            pager.setId(position);
            pager.setAdapter(new PagerCustomAdapter());

            pager.setCurrentItem(0);

            return row;
        }
    }
}
我尝试了两种
pagerAdapter
FragmentPagerAdapter
,结果都一样

我在
getView()
instantiateItem()
中添加了两条日志消息,这两条消息都按预期在logcat中读取。中既没有警告也没有错误。

我错过了什么

更新

活动的XML_main

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true" >
</ListView>

</RelativeLayout>

寻呼机项目列表的XML

<?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/pager"
         android:layout_width="match_parent"
         android:layout_height="match_parent" >
    </android.support.v4.view.ViewPager>
</LinearLayout>

我不确定你想达到什么目的。。是否要在事件发生时更改视图?比如当你点击它的时候?或者您希望得到与SwipeListView相同的结果

如果要更改视图,可以尝试使用
anc在事件发生时更改布局。但是我不确定这是否是您想要的结果

当在ListView行中使用ViewPager时,它需要有一个唯一的android:id(因为在每一行中必须有一个不同的id)。这是因为与ViewPager适配器关联的FragmentManager使用id和位置来存储片段。这意味着,如果只使用默认值,则会覆盖所有内容

下面是一个PagerAdapter示例,它允许您覆盖默认的makeFragmentName方法

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Parcelable;
import android.support.v13.app.FragmentCompat;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;

public abstract class TaggedFragmentPagerAdapter extends PagerAdapter {

    private final FragmentManager mFragmentManager;

    private FragmentTransaction mCurTransaction = null;

    private Fragment mCurrentPrimaryItem = null;

    public TaggedFragmentPagerAdapter(FragmentManager fm) {
        mFragmentManager = fm;
    }

    /**
     * Return the Fragment associated with a specified position.
     */
    public abstract Fragment getItem(int position);

    public String getItemTag(int position) {
        // Maintain backward compatibility
        return String.valueOf(getItemId(position));
    }

    @Override
    public void startUpdate(ViewGroup container) {
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        if (mCurTransaction == null) {
            mCurTransaction = mFragmentManager.beginTransaction();
        }

        // Do we already have this fragment?
        String name = makeFragmentName(position, container.getId(), getItemTag(position));
        Fragment fragment = mFragmentManager.findFragmentByTag(name);
        if (fragment != null) {
            // FIXME : Start fix.
            if (!fragment.isDetached()) {
                mCurTransaction.detach(fragment);
            }
            // FIXME : End fix.
            mCurTransaction.attach(fragment);
        } else {
            fragment = getItem(position);
            mCurTransaction.add(container.getId(), fragment, name);
        }
        if (fragment != mCurrentPrimaryItem) {
            FragmentCompat.setMenuVisibility(fragment, false);
            FragmentCompat.setUserVisibleHint(fragment, false);
        }

        return fragment;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        if (mCurTransaction == null) {
            mCurTransaction = mFragmentManager.beginTransaction();
        }
        mCurTransaction.detach((Fragment) object);
    }

    @Override
    public void setPrimaryItem(ViewGroup container, int position, Object object) {
        Fragment fragment = (Fragment) object;
        if (fragment != mCurrentPrimaryItem) {
            if (mCurrentPrimaryItem != null) {
                FragmentCompat.setMenuVisibility(mCurrentPrimaryItem, false);
                FragmentCompat.setUserVisibleHint(mCurrentPrimaryItem, false);
            }
            if (fragment != null) {
                FragmentCompat.setMenuVisibility(fragment, true);
                FragmentCompat.setUserVisibleHint(fragment, true);
            }
            mCurrentPrimaryItem = fragment;
        }
    }

    @Override
    public void finishUpdate(ViewGroup container) {
        if (mCurTransaction != null) {
            mCurTransaction.commitAllowingStateLoss();
            mCurTransaction = null;
            mFragmentManager.executePendingTransactions();
        }
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return ((Fragment) object).getView() == view;
    }

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

    @Override
    public void restoreState(Parcelable state, ClassLoader loader) {
    }

    /**
     * Return a unique identifier for the item at the given position.
     *
     * <p> The default implementation returns the given position. Subclasses should override this method if the positions of items
     * can change. </p>
     *
     * @param position Position within this adapter
     * @return Unique identifier for the item at position
     */
    public long getItemId(int position) {
        return position;
    }

    protected String makeFragmentName(int position, int viewId, String tagName) {
        return "android:switcher:" + viewId + ":" + tagName;
    }
}
导入android.app.Fragment;
导入android.app.FragmentManager;
导入android.app.FragmentTransaction;
导入android.os.Parcelable;
导入android.support.v13.app.FragmentCompat;
导入android.support.v4.view.PagerAdapter;
导入android.view.view;
导入android.view.ViewGroup;
公共抽象类TaggedFragmentPagerAdapter扩展了PagerAdapter{
私人最终碎片经理MFFragmentManager;
私有碎片事务mCurTransaction=null;
私有片段mCurrentPrimaryItem=null;
公共标记FragmentPageRadapter(碎片管理器fm){
MFFragmentManager=fm;
}
/**
*返回与指定位置关联的片段。
*/
公共抽象片段getItem(int位置);
公共字符串getItemTag(int位置){
//保持向后兼容性
返回字符串.valueOf(getItemId(position));
}
@凌驾
public void startUpdate(视图组容器){
}
@凌驾
公共对象实例化项(视图组容器,int位置){
if(mCurTransaction==null){
mCurTransaction=MFFragmentManager.beginTransaction();
}
//我们已经有这个片段了吗?
String name=makeFragmentName(位置,container.getId(),getItemTag(位置));
Fragment Fragment=MFFragmentManager.findFragmentByTag(名称);
if(片段!=null){
//修理我:开始修理。
如果(!fragment.isDetached()){
分离(碎片);
}
//修正我:结束修正。
连接(碎片);
}否则{
fragment=getItem(位置);
添加(container.getId(),片段,名称);
}
如果(片段!=mCurrentPrimaryItem){
FragmentCompat.setMenuVisibility(片段,false);
FragmentCompat.setUserVisibleHint(片段,false);
}
返回片段;
}
@凌驾
公共项(视图组容器、int位置、对象){
if(mCurTransaction==null){
mCurTransaction=MFFragmentManager.beginTransaction();
}
分离((片段)对象);
}
@凌驾
公共void setPrimaryItem(视图组容器、int位置、对象){
Fragment=(Fragment)对象;
如果(片段!=mCurrentPrimaryItem){
如果(mCurrentPrimaryItem!=null){
FragmentCompat.setMenuVisibility(mCurrentPrimaryItem,false);
FragmentCompat.setUserVisibleHint(mCurrentPrimaryItem,false);
}
if(片段!=null){
FragmentCompat.setMenuVisibility(片段,真);
FragmentCompat.setUserVisibleHint(fragment,true);
}
mCurrentPrimaryItem=片段;
}
}
@凌驾
public void finishUpdate(视图组容器){
如果(mCurTransaction!=null){
mCurTransaction.commitAllowingStateLoss();
mCurTransaction=null;
MFFragmentManager.executePendingTransactions();
}
}
@凌驾
公共布尔值isViewFromObject(视图,对象){
返回((片段)对象);
}
@凌驾
公共包裹存储状态(){
返回null;
}
@凌驾
公共无效恢复状态(可包裹状态,类装入器){
}
/**
*在给定位置返回项目的唯一标识符。
*
*默认实现返回给定的位置。如果项的位置
*可以改变

* *@param position此适配器内的位置 *@return位置处项目的唯一标识符 */ 公共长getItemId(int位置){ 返回位置; } 受保护的字符串makeFragmentName(int位置、int视图ID、字符串标记名){ 返回“android:switcher:+viewId+:”+标记名; } }
我遇到了一个类似的问题,我所做的是明确地设置了viewPager及其父项的高度。这对我有用

<?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="200dip"
    android:orientation="vertical" >

   <android.support.v4.view.ViewPager
      android:id="@+id/pager"
      android:layout_width="match_parent"
      android:layout_height="200dip" >
   </android.support.v4.view.ViewPager>
 </LinearLayout>

我不知道这是不是真的
<?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="200dip"
    android:orientation="vertical" >

   <android.support.v4.view.ViewPager
      android:id="@+id/pager"
      android:layout_width="match_parent"
      android:layout_height="200dip" >
   </android.support.v4.view.ViewPager>
 </LinearLayout>