屏幕适配
values的dimens
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="w">100dp</dimen>
</resources>
activity_main viewpager+fragment+侧滑的切换布局
<?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/drawer"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="9"
></android.support.v4.view.ViewPager>
<RadioGroup
android:id="@+id/group"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
>
<RadioButton
android:id="@+id/but1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:button="@null"
android:gravity="center"
android:text="影片"
/>
<RadioButton
android:id="@+id/but2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:button="@null"
android:gravity="center"
android:text="影院"
/>
<RadioButton
android:id="@+id/but3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:button="@null"
android:gravity="center"
android:text="会员"
/>
<RadioButton
android:id="@+id/but4"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:button="@null"
android:gravity="center"
android:text="设置"
/>
</RadioGroup>
</LinearLayout>
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
class="com.bwei.yuekaolianxi.LeftFragment"
/>
</android.support.v4.widget.DrawerLayout>
MainActivity
viewpager+Fragment切换的功能代码
public class MainActivity extends AppCompatActivity {
private ViewPager viewPager;
private RadioGroup radioGroup;
private List<Fragment> list;
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle toggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawerLayout=findViewById(R.id.drawer);
viewPager=findViewById(R.id.viewPager);
radioGroup=findViewById(R.id.group);
list=new ArrayList<>();
list.add(new FragmentOne());
list.add(new FragmentTwo());
list.add(new FragmentThree());
list.add(new FragmentFour());
toggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.open, R.string.close);
drawerLayout.addDrawerListener(toggle);
toggle.syncState();
viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
@Override
public Fragment getItem(int i) {
return list.get(i);
}
@Override
public int getCount() {
return list.size();
}
});
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId){
case R.id.but1:
viewPager.setCurrentItem(0);
break;
case R.id.but2:
viewPager.setCurrentItem(1);
break;
case R.id.but3:
viewPager.setCurrentItem(2);
break;
case R.id.but4:
viewPager.setCurrentItem(3);
break;
}
}
});
}
public void show(int positioin){
viewPager.setCurrentItem(positioin);
drawerLayout.closeDrawer(Gravity.START);
}
public boolean onOptionsItemSelected(MenuItem item){
if(toggle.onOptionsItemSelected(item)){
return true;
}
return super.onOptionsItemSelected(item);
}
侧滑的fragment的点击事件
public class LeftFragment extends Fragment {
private ListView listView;
private MainAdapter adapter;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.activity_left,container,false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
listView=view.findViewById(R.id.listView);
adapter = new MainAdapter(getActivity());
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(position==0){
return;
}
((MainActivity)getActivity()).show(position-1);
}
});
}
侧滑的布局
<ListView
android:id="@+id/listView"
android:layout_width="240dp"
android:layout_height="match_parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
></ListView>
侧滑的多条目加载 头像和listView
public class MainAdapter extends BaseAdapter {
private String[] data=new String[]{
"影片","影院","会员","设置"
};
private Context context;
public MainAdapter(Context context) {
this.context = context;
}
@Override
public int getCount() {
return data.length+1;
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public int getItemViewType(int position) {
return position==0?1:0;
}
@Override
public String getItem(int position) {
if(position==0){
return null;
}
return data[position-1];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(getItemViewType(positio
版权声明:本文为yjjhtl原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。