parent
01d47b965d
commit
c6bbe55d90
@ -0,0 +1,30 @@
|
||||
package com.example.leudaemialikeme.Adapter;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.fragment.app.Fragment;
|
||||
import androidx.fragment.app.FragmentManager;
|
||||
import androidx.fragment.app.FragmentPagerAdapter;
|
||||
|
||||
import com.example.leudaemialikeme.Fragment.InvitationChannelFragment;
|
||||
|
||||
public class InvitationPageFragmentAdapter extends FragmentPagerAdapter {
|
||||
private String[] channelList;
|
||||
private FragmentManager fm;
|
||||
public InvitationPageFragmentAdapter(@NonNull FragmentManager fm, String[] channelList) {
|
||||
super(fm);
|
||||
this.channelList = channelList;
|
||||
this.fm = fm;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public Fragment getItem(int position) {
|
||||
String InvitationCategoryTitle = channelList[position];
|
||||
return InvitationChannelFragment.newInstance(InvitationCategoryTitle);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCount() {
|
||||
return channelList.length;
|
||||
}
|
||||
}
|
@ -1,65 +1,107 @@
|
||||
package com.example.leudaemialikeme.Fragment;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.util.DisplayMetrics;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.HorizontalScrollView;
|
||||
import android.widget.RadioButton;
|
||||
import android.widget.RadioGroup;
|
||||
|
||||
import androidx.fragment.app.Fragment;
|
||||
import androidx.fragment.app.FragmentManager;
|
||||
import androidx.viewpager.widget.ViewPager;
|
||||
|
||||
import com.example.leudaemialikeme.Adapter.InvitationPageFragmentAdapter;
|
||||
import com.example.leudaemialikeme.R;
|
||||
|
||||
/**
|
||||
* A simple {@link Fragment} subclass.
|
||||
* Use the {@link CommunityFragment#newInstance} factory method to
|
||||
* create an instance of this fragment.
|
||||
*/
|
||||
public class CommunityFragment extends Fragment {
|
||||
|
||||
// TODO: Rename parameter arguments, choose names that match
|
||||
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
|
||||
private static final String ARG_PARAM1 = "param1";
|
||||
private static final String ARG_PARAM2 = "param2";
|
||||
|
||||
// TODO: Rename and change types of parameters
|
||||
private String mParam1;
|
||||
private String mParam2;
|
||||
public class CommunityFragment extends Fragment implements ViewPager.OnPageChangeListener {
|
||||
private static final String ARG_CHANNEL_LIST = "channel_list";
|
||||
private View view=null; // 碎片的布局实例
|
||||
private ViewPager viewPager; //内导航的碎片的容器
|
||||
private RadioGroup rgChannel=null; // 内导航由单选按钮组构成
|
||||
private HorizontalScrollView hvChannel=null; //单选按钮组可滚动动
|
||||
private String[] channelList = {"关注","全部","经验","扫雷","康复","科普","问答"}; //默认的内导航栏目
|
||||
private InvitationPageFragmentAdapter adapter; //viewPager 的适配器
|
||||
|
||||
public CommunityFragment() {
|
||||
// Required empty public constructor
|
||||
}
|
||||
|
||||
/**
|
||||
* Use this factory method to create a new instance of
|
||||
* this fragment using the provided parameters.
|
||||
*
|
||||
* @param param1 Parameter 1.
|
||||
* @param param2 Parameter 2.
|
||||
* @return A new instance of fragment CommunityFragment.
|
||||
*/
|
||||
// TODO: Rename and change types and number of parameters
|
||||
public static CommunityFragment newInstance(String param1, String param2) {
|
||||
CommunityFragment fragment = new CommunityFragment();
|
||||
Bundle args = new Bundle();
|
||||
args.putString(ARG_PARAM1, param1);
|
||||
args.putString(ARG_PARAM2, param2);
|
||||
fragment.setArguments(args);
|
||||
return fragment;
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||
Bundle savedInstanceState) {
|
||||
if(view==null){
|
||||
view=inflater.inflate(R.layout.fragment_community, container, false);
|
||||
viewPager=(ViewPager)view.findViewById(R.id.vpNewsList);
|
||||
initViewPager(); //设置 ViewPager
|
||||
|
||||
rgChannel=(RadioGroup)view.findViewById(R.id.rgChannel);
|
||||
hvChannel=(HorizontalScrollView)view.findViewById(R.id.hvChannel);
|
||||
initTab(inflater);
|
||||
|
||||
rgChannel.setOnCheckedChangeListener(
|
||||
new RadioGroup.OnCheckedChangeListener() {
|
||||
@Override
|
||||
public void onCheckedChanged(RadioGroup group, int checkedId) {
|
||||
viewPager.setCurrentItem(checkedId);
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
return view;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
if (getArguments() != null) {
|
||||
mParam1 = getArguments().getString(ARG_PARAM1);
|
||||
mParam2 = getArguments().getString(ARG_PARAM2);
|
||||
private void initTab(LayoutInflater inflater) {
|
||||
for(int i=0;i<channelList.length;i++){
|
||||
RadioButton rb=(RadioButton)inflater.inflate(R.layout.invitation_tab_rb,null);
|
||||
rb.setId(i);
|
||||
rb.setText(channelList[i]);
|
||||
RadioGroup.LayoutParams params = new
|
||||
RadioGroup.LayoutParams(RadioGroup.LayoutParams.WRAP_CONTENT,
|
||||
RadioGroup.LayoutParams.WRAP_CONTENT);
|
||||
|
||||
rgChannel.addView(rb,params);
|
||||
}
|
||||
rgChannel.check(0);
|
||||
}
|
||||
|
||||
private void initViewPager() {
|
||||
FragmentManager fragmentManager = super.getActivity().getSupportFragmentManager();
|
||||
adapter=new InvitationPageFragmentAdapter(fragmentManager, channelList);
|
||||
//参数 channelList 将被适配器用来在动态切换碎片的时候实时创建碎片 //设置 ViewPager 的适配器
|
||||
viewPager.setAdapter(adapter);
|
||||
viewPager.setOffscreenPageLimit(2);
|
||||
//设置显示第 1 个碎片
|
||||
viewPager.setCurrentItem(1);
|
||||
//设置 ViewPager 的切换监听
|
||||
viewPager.addOnPageChangeListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||
Bundle savedInstanceState) {
|
||||
// Inflate the layout for this fragment
|
||||
return inflater.inflate(R.layout.fragment_community, container, false);
|
||||
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPageSelected(int position) {
|
||||
setTab(position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPageScrollStateChanged(int state) {
|
||||
|
||||
}
|
||||
private void setTab(int idx){
|
||||
RadioButton rb=(RadioButton)rgChannel.getChildAt(idx);
|
||||
rb.setChecked(true);
|
||||
int left=rb.getLeft();
|
||||
int width=rb.getMeasuredWidth();
|
||||
DisplayMetrics metrics=new DisplayMetrics();
|
||||
super.getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
|
||||
int screenWidth=metrics.widthPixels;
|
||||
int len=left+width/2-screenWidth/2;
|
||||
hvChannel.smoothScrollTo(len,0);
|
||||
}
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
package com.example.leudaemialikeme.Fragment;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.fragment.app.Fragment;
|
||||
|
||||
import com.example.leudaemialikeme.R;
|
||||
|
||||
public class InvitationChannelFragment extends Fragment {
|
||||
private static final String ARG_CATEGORY_TITLE = "category_title";
|
||||
private String invitationCategoryTitle = "Default";
|
||||
private TextView mTitleField;
|
||||
|
||||
public InvitationChannelFragment() {
|
||||
// Required empty public constructor
|
||||
}
|
||||
|
||||
public static InvitationChannelFragment newInstance(String invitationCategoryTitle) {
|
||||
InvitationChannelFragment fragment = new InvitationChannelFragment();
|
||||
Bundle args = new Bundle();
|
||||
args.putString(ARG_CATEGORY_TITLE,invitationCategoryTitle);
|
||||
fragment.setArguments(args);
|
||||
return fragment;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
try{
|
||||
invitationCategoryTitle = (String)getArguments().getString(ARG_CATEGORY_TITLE);
|
||||
}catch (java.lang.NullPointerException e){
|
||||
System.out.println("TesFragment getArg error!");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||
Bundle savedInstanceState) {
|
||||
View view;
|
||||
view = inflater.inflate(R.layout.fragment_invitation_channel, container, false);
|
||||
mTitleField = (TextView)view.findViewById(R.id.invitationCategoryTitle);
|
||||
mTitleField.setText(invitationCategoryTitle);
|
||||
return view;
|
||||
}
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
|
||||
<item android:state_checked="true" >
|
||||
<layer-list >
|
||||
<item >
|
||||
<shape android:shape="rectangle">
|
||||
<stroke android:width="5dp" android:color="@color/hair_grey"/>
|
||||
</shape>
|
||||
|
||||
</item>
|
||||
<item android:bottom="5dp" >
|
||||
<shape android:shape="rectangle" >
|
||||
<solid android:color="#fff"/>
|
||||
</shape>
|
||||
</item>
|
||||
</layer-list>
|
||||
|
||||
|
||||
</item>
|
||||
<item android:state_selected="true" >
|
||||
<layer-list >
|
||||
<item >
|
||||
<shape android:shape="rectangle">
|
||||
<stroke android:width="5dp" android:color="@color/hair_grey"/>
|
||||
</shape>
|
||||
</item>
|
||||
<item android:bottom="5dp" >
|
||||
<shape android:shape="rectangle" >
|
||||
<solid android:color="#fff"/>
|
||||
</shape>
|
||||
</item>
|
||||
|
||||
</layer-list>
|
||||
</item>
|
||||
<item >
|
||||
<shape >
|
||||
<solid android:color="#ffF"/>
|
||||
</shape>
|
||||
</item>
|
||||
|
||||
</selector>
|
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<shape xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:shape="rectangle">
|
||||
<corners android:radius="25dp" />
|
||||
<padding
|
||||
android:bottom="3dp"
|
||||
android:left="10dp"
|
||||
android:right="5dp"
|
||||
android:top="3dp" />
|
||||
<stroke
|
||||
android:width="2dp"
|
||||
android:color="@color/light_grey"/>
|
||||
</shape>
|
@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
tools:context=".Fragment.InvitationChannelFragment">
|
||||
|
||||
<!-- TODO: Update blank fragment layout -->
|
||||
<TextView
|
||||
android:id="@+id/invitationCategoryTitle"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:gravity="center"
|
||||
android:textSize="30sp"
|
||||
android:text="@string/hello_blank_fragment" />
|
||||
|
||||
</FrameLayout>
|
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<RadioButton xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="30dp"
|
||||
android:text=""
|
||||
android:background="@drawable/invitation_tab_selector"
|
||||
android:paddingLeft="15dp"
|
||||
android:paddingRight="15dp"
|
||||
android:paddingTop="10dp"
|
||||
android:paddingBottom="10dp"
|
||||
android:button="@null"
|
||||
/>
|
||||
|
After Width: | Height: | Size: 1014 B |
After Width: | Height: | Size: 399 B |
After Width: | Height: | Size: 1.0 KiB |
After Width: | Height: | Size: 423 B |
Loading…
Reference in new issue