在Android中正确实现PagerAdapter

mpbci0fu  于 2022-12-31  发布在  Android
关注(0)|答案(3)|浏览(189)

我在实现自定义PagerAdapter并将其与ViewPager一起使用时遇到问题。此示例PagerAdapter有10个项目,每个项目都是一个按钮,其索引为文本。当我运行程序时,我看到一个文本为"1"而不是"0"的按钮。当我滑动到其他项目时,我只得到空白视图。当我向后滑动时,有时我看到一个带有一些数字的按钮。但是它消失了(也许它正在破坏,我把它从容器中移走了),有时我看到一个带数字的按钮,但是数字在滑动后发生了变化(我想我创建了一个新的按钮,我把它添加到容器中,由于某些原因,视图页显示了这个新按钮)。
我该如何修复这个实现呢?我在示例中没有看到差异。
我的PagerAdapter实现:

public class MyPagerAdapter extends PagerAdapter {

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

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

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        Button button = new Button(container.getContext());
        ViewGroup.LayoutParams params = new ActionBar.LayoutParams(-1,-1);
        button.setLayoutParams(params);
        button.setText(String.valueOf(position));
        container.addView(button);
        return button;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((Button)object);
    }
}

我的活动:

public class MainActivity extends ActionBarActivity {

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

        ViewPager pager = (ViewPager) findViewById(R.id.pager);
        pager.setAdapter(new MyPagerAdapter());
    }
}
weylhg0b

weylhg0b1#

下面是完整的代码:
xml布局:

<LinearLayout 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"
    android:orientation="vertical"
    tools:context="com.example.androidviewpagerapp.MainActivity" >

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

</LinearLayout>

我的寻呼机适配器类:

import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

public class MyPagerAdapter extends PagerAdapter {

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

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

    @Override
    public Object instantiateItem(final ViewGroup container, int position) {
        Button button = new Button(container.getContext());
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        button.setLayoutParams(params);
        button.setText(String.valueOf(position));

        final int page = position;
        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Toast.makeText(container.getContext(), "You clicked: " + page + ". page.", Toast.LENGTH_SHORT).show();
            }
        });

        container.addView(button);
        return button;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((Button)object);
    }
}

主要活动:

import android.support.v4.view.ViewPager;
import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {
    ViewPager viewPager;
    MyPagerAdapter myPagerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewPager = (ViewPager)findViewById(R.id.pager);
        myPagerAdapter = new MyPagerAdapter();
        viewPager.setAdapter(myPagerAdapter);
    }
}

你会看到按钮是全屏的。为了避免这种情况,你需要创建一些布局(如线性布局),并添加按钮到该布局。
示例:

import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

public class MyPagerAdapter extends PagerAdapter {

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

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

    @Override
    public Object instantiateItem(final ViewGroup container, int position) {
        Button button = new Button(container.getContext());
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        button.setLayoutParams(params);
        button.setText(String.valueOf(position));

        LinearLayout layout = new LinearLayout(container.getContext());
        layout.setOrientation(LinearLayout.VERTICAL);
        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

        //add buton to layout
        layout.addView(button);

        final int page = position;
        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Toast.makeText(container.getContext(), "You clicked: " + page + ". page.", Toast.LENGTH_SHORT).show();
            }
        });
        //to container add layout instead of button
        container.addView(layout);
        //return layout instead of button
        return layout;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        //cast to LinearLayout
        container.removeView((LinearLayout)object);
    }
}
n6lpvg4x

n6lpvg4x2#

如果你想在页面中展开视图,你必须实现两个方法。instantiateItem和destroyItem

public class DialogPagerAdapter extends PagerAdapter {

    private Context mContext;

    //view inflating..
    @Override
    public Object instantiateItem(ViewGroup collection, int position) {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        ViewGroup layout = (ViewGroup) inflater.inflate(R.layout.account_dialog_signin_viewpagers,
                collection, false);

        TextView tvLabel = (TextView) layout.findViewById(R.id.textView);
        switch (position) {
            case 0:
                tvLabel.setText("Log In");
                tvLabel.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                    }
                });
                break;
            case 1:
                tvLabel.setText("Sign Up");
                tvLabel.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                    }
                });
                break;
            case 2:
                tvLabel.setText("Send Reset Link");
                tvLabel.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        //onOptionClickForgot.OnOptionClick();
                    }
                });
                break;
        }

        collection.addView(layout);
        return layout;
    }

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

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

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

简单地说

viewPager.setAdapter(new DialogPagerAdapter);

可扩展标记语言

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="@dimen/dialog_button_height"
    android:paddingLeft="@dimen/dimen_2"
    android:paddingRight="@dimen/dimen_2"
    android:minHeight="@dimen/dialog_button_height">

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerInParent="true"
        android:gravity="center"
        android:text="@string/app_name"
        android:textColor="@color/white"
        android:textSize="@dimen/text_size_medium" />
</RelativeLayout>
jdzmm42g

jdzmm42g3#

在这里,我发布了TabLayoutViewPager之间的ViewPagerAdapter

public class ViewPagerAdapter extends FragmentPagerAdapter {

    ArrayList<String> titleList = new ArrayList<>();
    List<Fragment> fragment = new ArrayList<>();

    public void addFragment(String title, Fragment fragment) {
        this.titleList.add(title);
        this.fragment.add(fragment);
    }

    public ViewPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @NonNull
    @Override
    public Fragment getItem(int position) {
        return fragment.get(position);
    }

    @Override
    public int getCount() {
        return titleList.size();
    }

    public CharSequence getPageTitle(int position) {
        return titleList.get(position);
    }
}

如何使用ViewPagerAdapter

ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
 adapter.addFragment("TAB 1", new YOUR_FRAGMENT1());
 adapter.addFragment("TAB 2", new YOUR_FRAGMENT2());
 // Keep adding fragments.
 viewPager.setAdapter(adapter);
 tabLayout.setupWithViewPager(viewPager);

相关问题