1. 程式人生 > >TabLayout、 DrawerLayout側拉、Viewpager輪播圖:動態標題、view生成小圓點

TabLayout、 DrawerLayout側拉、Viewpager輪播圖:動態標題、view生成小圓點

package liyuanqi.bwie.com.chouti;

import android.support.design.widget.TabLayout; import android.support.v4.app.Fragment; import android.support.v4.view.ViewPager; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBar; import android.support.v7.app.ActionBarDrawerToggle; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.MenuItem; import android.view.View; import android.widget.ArrayAdapter; import android.widget.ListView;

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

public class MainActivity extends AppCompatActivity { List list; DrawerLayout mydrawerLayout; ListView lsv; ViewPager pagers; TabLayout tabs; List fragmentListlist; ActionBarDrawerToggle actionBarDrawerToggle; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lsv =findViewById(R.id.lsv); mydrawerLayout =findViewById(R.id.mydrawerLayout); pagers = findViewById(R.id.pagers); tabs = findViewById(R.id.tabs); listdata(); initbunttn(); tabandpager();

}

private void tabandpager() {
    myfragmadapter myfragmadapter = new myfragmadapter(getSupportFragmentManager());
     fragmentListlist=new ArrayList<>();
     fragmentListlist.add(new FramShouYe());
     fragmentListlist.add(new FramGeRen());
     myfragmadapter.fragnentlist(fragmentListlist);
     pagers.setAdapter(myfragmadapter);
     tabs.setTabMode(TabLayout.MODE_FIXED);
     tabs.addTab(tabs.newTab());
     tabs.addTab(tabs.newTab());
     tabs.setupWithViewPager(pagers);
     tabs.getTabAt(0).setText("主頁");
    tabs.getTabAt(1).setText("個人");
}

private void listdata() {
    list = new ArrayList<>();
    list.add("aaa");
    list.add("bbb");
    list.add("ccc");
    list.add("ddd");
    lsv.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,list));
}

private void initbunttn() {
    ActionBar actionBar = getSupportActionBar();
    actionBar.setDisplayHomeAsUpEnabled(true);
     actionBarDrawerToggle = new ActionBarDrawerToggle(this, mydrawerLayout, R.string.open, R.string.close);
    actionBarDrawerToggle.syncState();
    mydrawerLayout.addDrawerListener(actionBarDrawerToggle);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (actionBarDrawerToggle.onOptionsItemSelected(item)){
        return  true;
    }
    return super.onOptionsItemSelected(item);
}

}

package liyuanqi.bwie.com.chouti;

import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter;

import java.util.List;

class myfragmadapter extends FragmentPagerAdapter{ List myfragmentListlist; public myfragmadapter(FragmentManager supportFragmentManager) { super(supportFragmentManager);

}
    public void fragnentlist(List<Fragment> fragmentListlist) {
    this.myfragmentListlist=fragmentListlist;
}

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

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

}

package liyuanqi.bwie.com.chouti;

import android.support.v4.view.PagerAdapter; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView;

import java.util.List;

class MyLunbaoadapter extends PagerAdapter { List list; public void imageList(List imageViews) { this.list = imageViews; } @Override public int getCount() { return 10000; }

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

@Override
public Object instantiateItem(ViewGroup container, int position) {
    ImageView image=list.get(position%list.size());
    container.addView(image);
    return image;
}

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

}

package liyuanqi.bwie.com.chouti;

import android.annotation.SuppressLint; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView;

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

public class FramShouYe extends Fragment { LinearLayout viewlayout; ViewPager pagery; TextView zhu1; int[] tupian; List imageViews; String[] biaoti; MyLunbaoadapter myLunbaoadapter; Handler handler=new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg);

        int currentItem = pagery.getCurrentItem();
            currentItem++;
        pagery.setCurrentItem(currentItem);
        handler.sendEmptyMessageDelayed(1,1000);

    }
};
@Nullable
@Override
public View onCreateView(final LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = View.inflate(getActivity(), R.layout.framy, null);
     viewlayout = view.findViewById(R.id.viewlayout);
    pagery = view.findViewById(R.id.pagery);
    zhu1 = view.findViewById(R.id.zhu1);
    init();
    imagedata();
    pagery.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()){
                case MotionEvent.ACTION_MOVE:
                    handler.removeMessages(1);
                    break;
                case MotionEvent.ACTION_DOWN:
                    handler.removeMessages(1);
                    break;
                case MotionEvent.ACTION_UP:
                    handler.sendEmptyMessageDelayed(1,1000);
                    break;

            }
            return true;
        }
    });
    return view;
}
private void viewUpdate(int item) {
    zhu1.setText(biaoti[item]);
    for (int i=0;i<tupian.length;i++){
        View childAt = viewlayout.getChildAt(i);
        childAt.setBackgroundResource(item==i?R.drawable.dot_focus:R.drawable.dot_normal);
    }
}

@SuppressLint("ClickableViewAccessibility")
private void imagedata() {
    for (int i=0;i<tupian.length;i++){
        ImageView imageView = new ImageView(getActivity());
        imageView.setBackgroundResource(tupian[i]);
        imageViews.add(imageView);
        viewdata();
    }
    myLunbaoadapter = new MyLunbaoadapter();
    myLunbaoadapter.imageList(imageViews);
    pagery.setAdapter(myLunbaoadapter);
    pagery.setCurrentItem(1000);
    pagery.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            int item = pagery.getCurrentItem();
            viewUpdate(item%tupian.length);
        }

        @Override
        public void onPageSelected(int position) {

        }


        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });

    handler.sendEmptyMessageDelayed(0,1000);
}

private void viewdata() {
    View view = new View(getActivity());
    view.setBackgroundResource(R.drawable.dot_normal);
    LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(8,8);
    layoutParams.leftMargin=8;
    view.setLayoutParams(layoutParams);
    viewlayout.addView(view);
}

private void init() {
    imageViews=new ArrayList<>();
     tupian=new int[]{
            R.drawable.a,
            R.drawable.b,
            R.drawable.c,
            R.drawable.d,
            R.drawable.e,
    };
     biaoti=new String[]{
             "第一張",
             "第二張",
             "第三張",
             "第四張",
             "第五張",
     };

}

}

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

<android.support.v4.widget.DrawerLayout 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:id="@+id/mydrawerLayout" android:layout_height=“match_parent” tools:context=".MainActivity"> <android.support.design.widget.TabLayout android:id="@+id/tabs" android:layout_width=“match_parent” android:layout_weight=“1” android:layout_height=“0dp”></android.support.design.widget.TabLayout> <android.support.v4.view.ViewPager android:id="@+id/pagers" android:layout_width=“match_parent” android:layout_weight=“9” android:layout_height=“0dp”></android.support.v4.view.ViewPager>

</android.support.v4.widget.DrawerLayout>