修改不了ViewPager里面的值 50C

无论如何都修改不了ViewPager里面的值,尝试了各种控件,都是报错的结果
以下为XML文件

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.msi.lostfinder.MainActivity">


    <android.support.v4.view.ViewPager
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1">
    </android.support.v4.view.ViewPager>

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:background="?android:attr/windowBackground"
        app:menu="@menu/navigation" />

</LinearLayout>

以下为MainActivity代码

 package com.example.msi.lostfinder;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;


import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private ListView listLost;
    private String[] names = {"水杯", "手机", "钥匙", "USB线", "书籍", "饰品", "USB线", "U盘"};
    private int[] icons = {R.drawable.cup, R.drawable.phone, R.drawable.key, R.drawable.xian, R.drawable.cidian,
            R.drawable.wallet_woman, R.drawable.xian, R.drawable.upan};
    private View listview,history,map,user;
    private Button btn;
    private List<View> viewList;
    private ViewPager root;

    //底部菜单栏各个菜单项的点击事件处理
    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.navigation_LostFinder://失物招领
                    root.setCurrentItem(0);
                    return true;
                case R.id.navigation_History://历史信息
                    root.setCurrentItem(1);
                    return true;
                case R.id.navigation_Map://地图
                    root.setCurrentItem(2);
                    return true;
                case R.id.navigation_User://用户信息
                    root.setCurrentItem(3);
                    return true;
            }
            return false;
        }

    };
    public void init(){
        LayoutInflater inflater = getLayoutInflater();

        listLost =  inflater.inflate(R.layout.lost_items,null).findViewById(R.id.lost_item);
        MyBaseAdapter mAdapter = new MyBaseAdapter(this,names,icons);
        listLost.setAdapter(mAdapter);
        btn=inflater.inflate(R.layout.map,null).findViewById(R.id.button2);
        btn.setText("我能够动!");

    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {

            try{

                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                init();
                /*设置参数*/
                root = (ViewPager) findViewById(R.id.content);
                LayoutInflater inflater = getLayoutInflater();
                listview = inflater.inflate(R.layout.lost_items, null);
                history = inflater.inflate(R.layout.history, null);
                map = inflater.inflate(R.layout.map, null);
                user = inflater.inflate(R.layout.user, null);



                LayoutInflater layout=this.getLayoutInflater();
                View view=layout.inflate(R.layout.map, null);
                Button b=view.findViewById(R.id.button2);
                b.setText("我能动");
                TextView textview = (TextView) LayoutInflater.from(MainActivity.this).inflate(R.layout.history
                        , null).findViewById(R.id.textView);
                textview.setText("能改");


                /*将要分页显示的View装入数组中*/
                viewList = new ArrayList<>();
                viewList.add(listview);
                viewList.add(history);
                viewList.add(map);
                viewList.add(user);


                root.setAdapter(pagerAdapter);
                root.setCurrentItem(0);

                BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
                navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
            }
                catch(Exception ex){
                    ex.printStackTrace();
            }
    }
    public PagerAdapter pagerAdapter = new PagerAdapter() {

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            // TODO Auto-generated method stub
            //根据传来的key,找到view,判断与传来的参数View arg0是不是同一个视图
            return arg0 == arg1;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return viewList.size();
        }

        @Override
        public void destroyItem(ViewGroup container, int position,
                                Object object) {
            // TODO Auto-generated method stub
            container.removeView(viewList.get(position));
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            // TODO Auto-generated method stub
            container.addView(viewList.get(position));

            //把当前新增视图的位置(position)作为Key传过去
            return viewList.get(position);
        }

    };

}

1个回答

你把问题描述清楚一点吧,表示不懂是什么值。

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐