1. 程式人生 > >側滑!!!

側滑!!!

1.主頁+側滑佈局

<?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:layout_height="match_parent"
    tools:context=".MainActivity"
    android:id="@+id/drawer">
    <!-- 側滑頁面寫下面 -->
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        
      <android.support.design.widget.TabLayout
          android:id="@+id/bootm_tab"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          app:tabGravity="fill"
          android:layout_alignParentBottom="true">

      </android.support.design.widget.TabLayout>
        <android.support.v4.view.ViewPager
            android:id="@+id/viewpage"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        </android.support.v4.view.ViewPager>

    </RelativeLayout>
    <FrameLayout
        android:id="@+id/left111"
        android:layout_width="200dp"
        android:layout_height="match_parent"
        android:layout_gravity="start">
    </FrameLayout>
</android.support.v4.widget.DrawerLayout>

2.側滑頁面

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#f1f1">
    <ListView
        android:id="@+id/menus"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></ListView>

</LinearLayout>
照片
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:id="@+id/icon"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@mipmap/ic_launcher"
        />

</LinearLayout>
//文字
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="sss"
    android:textSize="30sp"
    android:id="@+id/lefttext"
    android:padding="10dp"/>

</LinearLayout>

3.主頁面

package com.bw.ymy.demo1;

import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.MenuItem;

import com.bw.ymy.demo1.Adapter.MinPage;
import com.bw.ymy.demo1.Fragment.leftFragment;

public class MainActivity extends AppCompatActivity {
    private DrawerLayout drawerLayout;
    private ActionBarDrawerToggle toogle;
    private ViewPager viewpage1;
    private TabLayout tabLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if(savedInstanceState==null)
        {
            getSupportFragmentManager().beginTransaction().add(R.id.left111,new leftFragment())
                    .commit();
            initView();


        }



    }
    private void initView(){
        //允許標題欄展示左邊icon
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        //獲取資源id
        drawerLayout=findViewById(R.id.drawer);
        //actionbar 和 DrawerLahyout中間人, 把中間人創建出來
        toogle=new ActionBarDrawerToggle(this,drawerLayout,R.string.open_drawer,R.string.close_drawer);
        drawerLayout.addDrawerListener(toogle);
        toogle.syncState();


        //獲取資源id
        viewpage1=findViewById(R.id.viewpage);
        //主頁面下面標題
       /* viewpage1.setAdapter(new MinPage(getSupportFragmentManager()));
        tabLayout=findViewById(R.id.bootm_tab);
        tabLayout.setupWithViewPager(viewpage1);
*/

    }
    public void showPage(int position) {
        viewpage1.setCurrentItem(position);
        drawerLayout.closeDrawer(Gravity.START);
    }

    //支援側滑點選
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(toogle.onOptionsItemSelected(item)) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}

4.leftAdapter

package com.bw.ymy.demo1.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bw.ymy.demo1.R;

public class leftAdapter extends BaseAdapter {
    private Context context;

    private  String[] menus=new String[]
            {
                    "首頁","電影","我的"
            };


    public leftAdapter(Context context) {
        this.context = context;
    }
    private final int ITEM_COUTN = 2;
    private final int IMAGE_TYPE = 0;
    private final int TEXT_TYPE = 1;
    @Override
    public int getCount() {
        return menus.length+1;
    }

    @Override
    public int getViewTypeCount() {
        return ITEM_COUTN;
    }

    @Override
    public int getItemViewType(int position) {
        return position == 0 ? IMAGE_TYPE : TEXT_TYPE;
    }

    @Override
    public String getItem(int position) {
        if(position==0)
        {
            return null;

        }

        return menus[position-1];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder=null;
        if(convertView==null)
        {
            convertView=LayoutInflater.from(context).inflate(getItemViewType(position)==IMAGE_TYPE?R.layout.left_image:R.layout.left_text,parent,false);
            holder=new ViewHolder(convertView);
        }else
        {
            holder = (ViewHolder) convertView.getTag();
        }
        if(getItemViewType(position)==TEXT_TYPE)
        {
            holder.bind(getItem(position));
        }
        return convertView;
    }
    //建立季軍器
    class ViewHolder{
        ImageView icon;
        TextView lefttext;
        //獲取資源id
        public  ViewHolder(View convertView)
        {
            icon=convertView.findViewById(R.id.icon);
            lefttext=convertView.findViewById(R.id.lefttext);
            convertView.setTag(this);
        }
        //複製
        public  void bind(String item)
        {
            lefttext.setText(item);
        }
    }
}

5.leftFragment

package com.bw.ymy.demo1.Fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.bw.ymy.demo1.Adapter.leftAdapter;
import com.bw.ymy.demo1.R;

public class leftFragment extends Fragment {
    private ListView listview;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.left_fragment,container,false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        //獲取資源id
        listview=view.findViewById(R.id.menus);
        //介面卡
        listview.setAdapter(new leftAdapter(getActivity()));


    }
}