简体   繁体   English

如何在ViewPager中访问项目(对象)?

[英]How to get access to item (object) in a ViewPager?

I've created an acitvity that must have two pages. 我创建了一个必须有两页的活动。 Those two pages shouldn't work like two different and unrelated activities, but like, let's say, one activity with two work area. 这两个页面不应该像两个不同且不相关的活动一样工作,而应该说,一个活动具有两个工作区域。 Switching between them just with sliding right or left. 左右滑动即可在它们之间切换。 So I've impleneted it and switching works, but I can't get access to the object inside ViewPager . 因此,我已经实现了它并进行了切换,但是我无法访问ViewPager的对象。 Description: Parent activity activity_setdetail.xml has two area: list view_setdetail_1.xml - of items and view_setdetail_2.xml - some settings. 说明:父活动activity_setdetail.xml具有两个区域:列表view_setdetail_1.xml项目和view_setdetail_2.xml一些设置。 So, those two areas relate to different *.xml . 因此,这两个区域涉及不同的*.xml

XMLs 个XML

activity_setdetail.xml activity_setdetail.xml

    <android.support.v4.view.ViewPager
        android:id="@+id/vpager"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="2dp"
        android:layout_marginEnd="2dp"
        android:layout_marginLeft="2dp"
        android:layout_marginRight="2dp"
        android:layout_marginStart="2dp"
        android:layout_marginTop="2dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

view_setdetail_1.xml it goes to ViewPager vpager as right side area view_setdetail_1.xml,它作为右侧区域转到ViewPager vpager

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:id="@+id/listViewOfWords"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="2dp"
        android:layout_marginLeft="2dp"
        android:layout_marginRight="2dp"
        android:layout_marginStart="2dp"
        android:layout_marginTop="8dp"
        android:clickable="true"
        android:focusable="true"
        android:paddingBottom="20dp"
        android:textAlignment="center"
        app:layout_constraintBottom_toTopOf="@+id/txtToLBL"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


    <TextView
        android:id="@+id/txtToLBL"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:text="@string/view_goToSettings"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent" />

</android.support.constraint.ConstraintLayout>

view_setdetail_2.xml it goes to ViewPager vpager as left side area view_setdetail_2.xml,它作为左侧区域转到ViewPager vpager

   <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/txtCardsQTYinLessonlbl"
        android:layout_width="0dp"
        android:layout_height="38dp"
        android:layout_marginLeft="2dp"
        android:layout_marginStart="2dp"
        android:layout_marginTop="2dp"
        android:gravity="center_vertical"
        android:text="@string/txtCardsQTYinLessonLBLString"
        android:textAlignment="center"
        android:textColor="?attr/editTextColor"
        android:textSize="16sp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/etxtCardsQTYinLesson"
        android:layout_width="34dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="2dp"
        android:layout_marginLeft="2dp"
        android:layout_marginRight="2dp"
        android:layout_marginStart="2dp"
        android:layout_marginTop="2dp"
        android:ems="10"
        android:imeOptions="actionDone"
        android:inputType="number"
        android:text="99"
        android:textAlignment="center"
        android:textSize="14sp"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toRightOf="@+id/txtCardsQTYinLessonlbl"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/txtTolistLBL"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:text="@string/view_goToList"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent" />

</android.support.constraint.ConstraintLayout>

Resource file that enumerate screens for activity_setdetail.xml 枚举activity_setdetail.xml的屏幕的资源文件

public enum ResourcesModel {


    FIRST_SCREEN(R.string.view_setdetail_1, R.layout.view_setdetail_1), SECOND_SCREEN(R.string.view_setdetail_2, R.layout.view_setdetail_2);

    private int mTitleResourceId;
    private int mLayoutResourceId;

    ResourcesModel(int titleResId, int layoutResId) {
        mTitleResourceId = titleResId;
        mLayoutResourceId = layoutResId;
    }

    public int getTitleResourceId() {
        return mTitleResourceId;
    }

    public int getLayoutResourceId() {
        return mLayoutResourceId;
    }
}

Adapter: 适配器:

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import am.slotvin.hoodiesoft.by.enums.ResourcesModel;

public class SimplePagerAdapter extends PagerAdapter {

    private Context mContext;

    public SimplePagerAdapter(Context context) {
        mContext = context;
    }

    @Override
    public Object instantiateItem(ViewGroup viewGroup, int position) {
        ResourcesModel resources = ResourcesModel.values()[position];
        LayoutInflater inflater = LayoutInflater.from(mContext);
        ViewGroup layout = (ViewGroup) inflater.inflate(
                resources.getLayoutResourceId(), viewGroup, false);
        viewGroup.addView(layout);
        return layout;
    }

    @Override
    public void destroyItem(ViewGroup viewGroup, int position, Object view) {
        viewGroup.removeView((View) view);
    }

    @Override
    public int getCount() {
        return ResourcesModel.values().length;
    }

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

    @Override
    public CharSequence getPageTitle(int position) {
        ResourcesModel customPagerEnum = ResourcesModel.values()[position];
        System.out.println("SimplePagerAdapter CharSequence getPageTitle" + position);
        return mContext.getString(customPagerEnum.getTitleResourceId());
    }

}

Java class for activity_setdetail.xml . activity_setdetail.xml的 Java类。 I've reduced code just to only textview . 我将代码简化为仅textview But in reality I have ListView there and other stuff. 但实际上,我在那里有ListView和其他东西。 Nevertheless, I can't influence even to textview . 尽管如此,我什至不能影响textview

public class SetDetail extends AppCompatActivity {

    private String setName;
    private int setID;
    TextView txtToLBL;

    SimplePagerAdapter spa;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setName = getIntent().getStringExtra("setName");
        setID = getIntent().getIntExtra("setID", 0);
        setTitle(this.getString(R.string.activity_setdetailLabel) + setName);
        setContentView(R.layout.activity_setdetail);

        spa = new SimplePagerAdapter(this);

        ViewPager viewPager = (ViewPager) findViewById(R.id.vpager);
        if (viewPager != null) {
            viewPager.setAdapter(spa);

        }

        txtToLBL = (TextView) findViewById(R.id.txtToLBL); //doesn't work
        //txtToLBL.setText("bla-bla-bla"); //error - android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference

        LayoutInflater inflater = LayoutInflater.from(this);
        View v = inflater.inflate(R.layout.view_setdetail_1, null, false);
        txtToLBL = (TextView) v.findViewById(R.id.txtToLBL);
        txtToLBL.setText("bla-bla-bla");//also doesn't work. No errors, just no changes.

    }

}

So the question is - How to gegt access to elements on views that loaded in a ViewPager ? 所以,问题是-如何gegt访问,在一个ViewPager装上的看法元素? I found similar questions, but they didn't work for me. 我发现了类似的问题,但它们对我没有用。

UPDATE 1: So, I've completely moved all code (also a bunch of listeners) to my SimplePagerAdapter . 更新1:因此,我已经将所有代码(也是一堆侦听器)完全移到了SimplePagerAdapter Now it should be renamed, because it is not universal anylonger and closely-coupled with the java class of parent activity. 现在应该重命名它,因为它不再通用,并且与父活动的java类紧密耦合。 Looks not very good, but the only way I found at the moment. 看起来不是很好,但这是我目前发现的唯一方法。 Though works OK, without freezing. 虽然可以,但不会冻结。 Still be good to hear more elegant solution. 还是很高兴听到更优雅的解决方案。

A ViewPager inflates 3 fragments at a time, the current fragment in view and a fragment on both left and right side of it. ViewPager一次放大3个片段,当前的片段在视图中,左侧和右侧都有一个片段。 Since, your ViewPager has only two fragments both are inflated. 因为,您的ViewPager只有两个片段都被夸大了。 So you can use something like viewPager.findViewById(resId) where resId is the Id of the view you want to access 因此,您可以使用诸如viewPager.findViewById(resId) ,其中resId是您要访问的视图的ID

I have the same kind of issue, the viewPager and the PagerAdapter should have been designed with a callback for when it's finished loading it's views. 我有同样的问题,viewPager和PagerAdapter应该在完成加载视图时设计了一个回调。 If I put code I want to run inside onPageSelected it all works, its just not available onCreate or onResume (presumably because the view is not instantiated by the pager yet.. If I had hair I'd be pulling it out trying to set default text on TextViews when my activity is Created... 如果我想在onPageSelected中运行代码,那么所有功能都可以正常运行,它只是在onCreate或onResume中不可用(大概是因为该视图尚未被分页器实例化。)如果我有头发,我会尝试将其设置为默认值创建我的活动时在TextViews上显示文本...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM