2 freedom wings3 freedom_wings3 于 2016.02.12 18:18 提问

安卓,可循环滑动的图片,不显示标示哪张图的小点,求大神来帮忙

contentmain.xml:

 <?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/activity_main" tools:context=".MainActivity">

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

    <LinearLayout
        android:id="@+id/viewgroup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_gravity="center_horizontal"
        android:layout_marginBottom="10dp"></LinearLayout>
</FrameLayout>

定义点状图标的drawable的xml,以下是选中的,没选中的也就颜色不一样:

 <?xml version="1.0" encoding="utf-8"?>


    <shape
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:color="#ff6666"
        android:shape="ring">

    <corners
        android:radius="20dp"
        />

    </shape>

viewpager是用fragment填充的,fragment里也就一个imageButton,不贴了

以下是MainActivity:

 public class MainActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener {
    List<Fragment> list;
    ViewPager viewPager;
    ViewGroup viewGroup;
    Fragment1 f1=new Fragment1();
    Fragment1 p1=new Fragment1();
    Fragment2 f2=new Fragment2();
    Fragment3 f3=new Fragment3();
    Fragment4 f4=new Fragment4();
    Fragment4 p4=new Fragment4();
    MyAdapter myAdapter;
    ImageView[] imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
        list=new ArrayList<Fragment>();
        list.add(p4);
        list.add(f1);
        list.add(f2);
        list.add(f3);
        list.add(f4);
        list.add(p1);
        viewPager=(ViewPager)super.findViewById(R.id.viewpager);
        viewGroup=(ViewGroup)super.findViewById(R.id.viewgroup);
        initViewGroup();
        myAdapter=new MyAdapter(getSupportFragmentManager(),list);
        viewPager.setAdapter(myAdapter);
        viewPager.setCurrentItem(1);
        viewPager.setOnPageChangeListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
    public void initViewGroup()
    {
        imageView=new ImageView[list.size()-3];
        for(int i=0;i<list.size()-3;i++)
        {
            imageView[i] = new ImageView(this);

            if(i==1) imageView[i].setBackgroundResource(R.drawable.select);
            else imageView[i].setBackgroundResource(R.drawable.unselect);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            params.leftMargin = 10;
            params.rightMargin = 10;
            viewGroup.addView(imageView[i], params);
        }
    }

    @Override
    public void onPageScrollStateChanged(int args0)
    {

    }
    public void onPageSelected(int args0)
    {
        int pageIndex=args0;
        if(pageIndex==0)
        {
            pageIndex=list.size()-2;
            imageView[0].setBackgroundResource(R.drawable.unselect);
            imageView[imageView.length-1].setBackgroundResource(R.drawable.select);
        }
        else if(pageIndex==list.size()-1)
        {
            pageIndex=1;
            imageView[imageView.length-1].setBackgroundResource(R.drawable.unselect);
            imageView[0].setBackgroundResource(R.drawable.select);
        }

        if(pageIndex != args0)
        {
            viewPager.setCurrentItem(pageIndex, false);
        }

    }
    public void onPageScrolled(int a1,float a2,int a3)
    {

    }
}

想做那种图片可循环滑动,并且其底部有小点标示是哪张图的效果,现在图片能循环滑,但是不显示小点,希望各位大神帮忙看看

4个回答

freedom_wings3
freedom_wings3   2016.02.12 18:21

补充一句,initViewGroup这个方法是定义小点的,其中R.drawable.select是我上面贴的定义点状的xml

daweibalang717
daweibalang717   2016.02.14 17:47

android:shape="ring"
>
/>

daweibalang717
daweibalang717   2016.02.14 17:47
 <shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="ring"
    >
    <solid   android:color="#ff6666"
            />

</shape>
crazy1235
crazy1235   Ds   Rxr 2016.02.18 11:03
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!