//先上效果图
//以下是简写,不带布局文件,最下面还有详细的,带布局文件的:
//———————-直接上代码Activity页面———————————-
//这个类是继承的BaseActivity
public class YueDouShanCheng extends BaseActivity{
private ListView mListView;
private FrameLayout mYueDouFrame;
private FragmentManager supportFragmentManager;
private FragmentTransaction transaction;
private List<yuedoubaen.ResultListBean> resultList;
private List<Fragment>mFragmentList=new ArrayList<>();
private MyListViewApader apader;
private ImageView mYueDouFanhui;
//—————–初始化控件—————–
@Override
protected void initView() {
mListView = findViewById(R.id.mListView);
mYueDouFrame = findViewById(R.id.mYueDouFrame);
mYueDouFanhui = findViewById(R.id.mYueDouFanhui);
mYueDouFanhui.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
//———————————布局————————————-
@Override
protected int getActivityLayout() {
return R.layout.yuedoulayout;
}
@Override
protected void initData() {
//---------------------------------fragment管理器
supportFragmentManager = getSupportFragmentManager();
transaction = supportFragmentManager.beginTransaction();
//get 异步
OkHttpClient client = new OkHttpClient.Builder().build();
Request request = new Request.Builder().url("https://api.dyhoa.com/dapi/v4/mall").method("GET", null).build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
//请求失败
}
@Override
public void onResponse(Call call, Response response) throws IOException {
//请求成功
String string = response.body().string();
Gson gson = new Gson();
yuedoubaen yuedoubaen = gson.fromJson(string, yuedoubaen.class);
resultList = yuedoubaen.getResultList();
runOnUiThread(new Runnable() {
@Override
public void run() {
//----------------------------------左边listview适配器,请求回来的数据,适配左边的listview----------------------
apader = new MyListViewApader(resultList,YueDouShanCheng.this);
mListView.setAdapter(apader);
//--------------------------------fragment复用与添加到fragment管理器的方法 initData2()--------------------------
initData2();
//------------------------------------默认切换到第一个item的fragment界面---------------------------------------
replese(0);
//-------------------------------------默认选中listview第一行-----------------------------------------------
resultList.get(0).setSelect(true);
}
});
}
});
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//---------------------------------点击item通过postation切换页面-------------------------------------------
replese(position);
//在bean类里写的一个标记 boolean类型的isSelect是关键,默认无状态, 并设置get set方法
//一进点击事件默认把所有item的isSelect设为false 表示都没选中
for (int i = 0; i <resultList.size() ; i++) {
yuedoubaen.ResultListBean resultListBean = resultList.get(i);
resultListBean.setSelect(false);
}
//点哪个那个为true 表示选中
resultList.get(position).setSelect(true);
//在刷新一下适配器就ok
apader.notifyDataSetChanged();
}
});
}
//复用
private void initData2() {
//定义了个数组,这些url是右边fragment的数据
String str[]={
"https://api.dyhoa.com/dapi/v4/mall/productList?nextPage=1&terminal=1601&sign=bad6045f74be22701cca6caee2a16f38&signature=8aa6882f38aae1d39a26d26149834f7fb3e52f03&categoryId=11&pageSize=10&version=3.6.8.9&phone=18201222438×tamp=1530752569&os=1501&nonceStr=5513c36e7c334dd20ab0ffeac130dca8",
"https://api.dyhoa.com/dapi/v4/mall/productList?nextPage=1&terminal=1601&sign=eab90cdfe6055b9e6bf05042a44c6ece&signature=b8349fbdaf039fc5a5ff9271f0303000370d05dd&categoryId=12&pageSize=10&version=3.6.8.9&phone=18201222438×tamp=1530752665&os=1501&nonceStr=43f8e83d15ca083fb58e9b745e5de0d4",
"https://api.dyhoa.com/dapi/v4/mall/productList?nextPage=1&terminal=1601&sign=d3feedfa2296fdea2929993e8393fdcc&signature=0e4ccfa9ec0a11d4377e416f803c6f0ab113563d&categoryId=21&pageSize=10&version=3.6.8.9&phone=18201222438×tamp=1530752732&os=1501&nonceStr=2aec405d4b5959235c49ec1d78edb0c2",
"https://api.dyhoa.com/dapi/v4/mall/productList?nextPage=1&terminal=1601&sign=3e9b48daa39e1055de40dd65ad424014&signature=aff890653c188a8c3fa72eee7695d09158518dbb&categoryId=13&pageSize=10&version=3.6.8.9&phone=18201222438×tamp=1530752814&os=1501&nonceStr=8068fee5f49946b3a8f85b1007cd40bb",
"https://api.dyhoa.com/dapi/v4/mall/productList?nextPage=1&terminal=1601&sign=c9f0f7e11561fbec4e05e00965c8b8c7&signature=b6919e18067363d7b5fd2335dfac3338b132e731&categoryId=14&pageSize=10&version=3.6.8.9&phone=18201222438×tamp=1530752855&os=1501&nonceStr=ee8374ec4e4ad797d42350c904d73077",
"https://api.dyhoa.com/dapi/v4/mall/productList?nextPage=1&terminal=1601&sign=1efb492116e7fece7753bc4ab0cdc667&signature=83f6ba42c465b22be873257a7e8c6652e430d61a&categoryId=15&pageSize=10&version=3.6.8.9&phone=18201222438×tamp=1530752915&os=1501&nonceStr=a29d1598024f9e87beab4b98411d48ce",
"https://api.dyhoa.com/dapi/v4/mall/productList?nextPage=1&terminal=1601&sign=216d3d071ce20d33d76e0931cc3e3f54&signature=13a7da4784a5aa1213a33c800680b5531cbeafbf&categoryId=16&pageSize=10&version=3.6.8.9&phone=18201222438×tamp=1530752954&os=1501&nonceStr=5d293363c4be77f134214bec786e2feb",
"https://api.dyhoa.com/dapi/v4/mall/productList?nextPage=1&terminal=1601&sign=9ced84694331e90a565a6cae395734f6&signature=ed9cb7e7273de3855cb20ce813122c69d2836679&categoryId=17&pageSize=10&version=3.6.8.9&phone=18201222438×tamp=1530752986&os=1501&nonceStr=3636638817772e42b59d74cff571fbb3",
"https://api.dyhoa.com/dapi/v4/mall/productList?nextPage=1&terminal=1601&sign=dd41a6a3c2294d745d86237c88959090&signature=6c234dfb3cf8b513ae34a0709897a866f72f216f&categoryId=19&pageSize=10&version=3.6.8.9&phone=18201222438×tamp=1530753009&os=1501&nonceStr=b9f35816f460ab999cbc168c4da26ff3",
"https://api.dyhoa.com/dapi/v4/mall/productList?nextPage=1&terminal=1601&sign=baebde57d5f169a2fd2b247d9657a405&signature=bace9e592dd311691003c82c9f166570a5b47b48&categoryId=22&pageSize=10&version=3.6.8.9&phone=18201222438×tamp=1530753050&os=1501&nonceStr=5680522b8e2bb01943234bce7bf84534",
"https://api.dyhoa.com/dapi/v4/mall/productList?nextPage=1&terminal=1601&sign=7f763ad694cd17eb4d713b2c82ff66b0&signature=744e9d8e6f79929134a3716303c78f2e16ad90fe&categoryId=23&pageSize=10&version=3.6.8.9&phone=18201222438×tamp=1530753084&os=1501&nonceStr=07042ac7d03d3b9911a00da43ce0079a"
};
//--------------------这里是复用,数组多大,复用多少,并传递过去resultList里实体bean类的一个Name,
YueDouFragment yueDouFragment = new YueDouFragment();
for (int i = 0; i <str.length ; i++) {
//------------------------------传递了集合的Name属性,和数组的url地址-------------------------------------
Fragment fuYong = yueDouFragment.getFuYong(resultList.get(i).getName(), str[i]);
//------------------------------集合添加这个fragment--------------------------------------------------
mFragmentList.add(fuYong);
}
//----------------------------for循环mFragmentList集合的长度,并添加到fragment管理器里----------------------
for (int i = 0; i < mFragmentList.size(); i++) {
transaction.add(R.id.mYueDouFrame,mFragmentList.get(i));
}
//----------------------------------------提交----------------------------------------------------
transaction.commitAllowingStateLoss();
}
//-------------------------------切换fragment的方法,通过postation--------------------------------
private void replese(int position) {
//重新得到了一个transaction 对象
FragmentTransaction transaction = supportFragmentManager.beginTransaction();
//-----------------------------------for循环全部隐藏--------------------------------------------------------
for (int i = 0; i <mFragmentList.size() ; i++) {
Fragment fragment = mFragmentList.get(i);
transaction.hide(fragment);
}
//点击的哪个下标,显示哪个fragment
switch (position){
case 0:
transaction.show(mFragmentList.get(0)).commitAllowingStateLoss();
break;
case 1:
transaction.show(mFragmentList.get(1)).commitAllowingStateLoss();
break;
case 2:
transaction.show(mFragmentList.get(2)).commitAllowingStateLoss();
break;
case 3:
transaction.show(mFragmentList.get(3)).commitAllowingStateLoss();
break;
case 4:
transaction.show(mFragmentList.get(4)).commitAllowingStateLoss();
break;
case 5:
transaction.show(mFragmentList.get(5)).commitAllowingStateLoss();
break;
case 6:
transaction.show(mFragmentList.get(6)).commitAllowingStateLoss();
break;
case 7:
transaction.show(mFragmentList.get(7)).commitAllowingStateLoss();
break;
case 8:
transaction.show(mFragmentList.get(8)).commitAllowingStateLoss();
break;
case 9:
transaction.show(mFragmentList.get(9)).commitAllowingStateLoss();
break;
case 10:
transaction.show(mFragmentList.get(10)).commitAllowingStateLoss();
break;
}
}
}
//—————————–复用的fragment页面———————————–
//这个类是继承的BaseFragment
public class YueDouFragment extends BaseFragment{
private String name;
private String name2;
private TextView mYeDouText;
private RecyclerView mYueDouRecy;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//------------在这里得到从下面的bundle里传来的两个string,并赋值给name和name2,并全局化.---------
Bundle arguments = getArguments();
if (arguments != null) {
name = arguments.getString("string");
name2 = arguments.getString("string2");
}
}
@Override
protected void initView(View view) {
mYeDouText = view.findViewById(R.id.mYeDouText);
mYueDouRecy = view.findViewById(R.id.mYueDouRecy);
}
@Override
protected int getFragmentLayout() {
return R.layout.yuedoufragmentlayout;
}
//复用的静态方法,两个参数都是String类型的,代表从Activity里传过来的一个集合的Name,一个数组的url地址
public static Fragment getFuYong(String string,String string2) {
YueDouFragment yueDouFragment = new YueDouFragment();
Bundle bundle = new Bundle();
//得到并赋值给string和string2,通过bundle传递
bundle.putString("string", string);
bundle.putString("string2", string2);
yueDouFragment.setArguments(bundle);
return yueDouFragment;
}
@Override
protected void initData() {
//get 异步
OkHttpClient client = new OkHttpClient.Builder().build();
//--------------------------------通过传过来的name2发起网络请求-------------------------------------------
Request request = new Request.Builder().url(name2).method("GET", null).build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
//请求失败
}
@Override
public void onResponse(Call call, Response response) throws IOException {
//请求成功
String string = response.body().string();
Gson gson = new Gson();
final YueDouBean2 yueDouBean2 = gson.fromJson(string, YueDouBean2.class);
//-----------------------这个p_count 是每个fragment里的item数量-------------------------------------------
final int p_count = yueDouBean2.getResult().getP_count();
final List<YueDouBean2.ResultListBean.ListBean> list = yueDouBean2.getResultList().getList();
MyApp.mContext.runOnUiThread(new Runnable() {
@Override
public void run() {
//传过来的name,也就是左边listview的name,加上每个fragment里的item数量,赋值给mYeDouText,例如上图里的“礼卡礼卷(4)”
mYeDouText.setText(name+"("+p_count+")");
//------------------------------然后RecyclerView适配器,适配里面的数据------------------------
LinearLayoutManager manager = new LinearLayoutManager(getContext());
mYueDouRecy.setLayoutManager(manager);
YueDouApader yueDouApader = new YueDouApader(R.layout.yuedouapaderlayout, list);
mYueDouRecy.setAdapter(yueDouApader);
}
});
}
});
}
}
//listview适配器,还得实现一个点击哪个,item变成白色,还出现一个红色的竖条。
public class MyListViewApader extends BaseAdapter {
private List<yuedoubaen.ResultListBean> mlist;
private Context context;
public MyListViewApader(List<yuedoubaen.ResultListBean> mlist, Context context) {
this.mlist = mlist;
this.context = context;
}
@Override
public int getCount() {
return mlist.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@SuppressLint("ViewHolder")
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView==null){
holder=new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.listviewlayout, null);
holder.mListViewText=convertView.findViewById(R.id.mListViewText);
holder.mYueDouXianShi = convertView.findViewById(R.id.mYueDouXianShi);
holder.mYueDouLin=convertView.findViewById(R.id.mYueDouLin);
convertView.setTag(holder);
}else {
holder= (ViewHolder) convertView.getTag();
}
String name = mlist.get(position).getName();
holder.mListViewText.setText(name);
//true和false用来解决复用问题的,如果当前标记为选中状态那么item字体颜色变红,红色竖条显示,item背景色变白
if (mlist.get(position).isSelect()){
holder.mListViewText.setTextColor(Color.RED);
holder.mYueDouXianShi.setVisibility(View.VISIBLE);
holder.mYueDouLin.setBackgroundColor(Color.WHITE);
}
//如果不是选中状态,item字体颜色变黑,红色竖条隐藏,item背景还变灰
else {
holder.mListViewText.setTextColor(Color.BLACK);
holder.mYueDouXianShi.setVisibility(View.GONE);
holder.mYueDouLin.setBackgroundColor(Color.parseColor("#EBEBEB"));
}
return convertView;
}
public static class ViewHolder {
public TextView mListViewText;
public TextView mYueDouXianShi;
public LinearLayout mYueDouLin;
}
}
//以下是一个详细的比较简单的:
//第一步 我的Activity布局:
<?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:orientation="horizontal"
>
<ListView
android:background="#EBEBEB"
android:id="@+id/mListView"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent"/>
<FrameLayout
android:id="@+id/mFrame"
android:layout_weight="8"
android:layout_width="0dp"
android:layout_height="match_parent"/>
</LinearLayout>
//第二步 我的Activity代码实现:
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {
private ListView mListView;
private FrameLayout mFrame;
private List<User> mList = new ArrayList<>();
private List<Fragment> mFragmentList = new ArrayList<>();
private FragmentManager supportFragmentManager = getSupportFragmentManager();
private MyListViewApader apader;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
}
private void initView() {
mListView = (ListView) findViewById(R.id.mListView);
mFrame = (FrameLayout) findViewById(R.id.mFrame);
}
private void initData() {
//左边listView集合添加数据,适配器适配
listViewData();
//添加fragment,复用fragment
addFragment();
//默认选中ListView第一条item
replese(0);
//ListView第一条item的Select为true
mList.get(0).setSelect(true);
//listView点击事件
mListView.setOnItemClickListener(this);
}
/*
* 方法名: listViewData()
* 功 能:左边listView集合添加数据
* 参 数:无
* 返回值:无
*/
private void listViewData() {
for (int i = 0; i < 10; i++) {
mList.add(new User("页面" + i));
}
//适配器适配
apader = new MyListViewApader(mList, this);
mListView.setAdapter(apader);
}
/*
* 方法名:addFragment()
* 功 能:添加fragment,复用fragment
* 参 数:无
* 返回值:无
*/
private void addFragment() {
FragmentTransaction transaction = supportFragmentManager.beginTransaction();
MultiplexingFragment multiplexingFragment = new MultiplexingFragment();
for (int i = 0; i < mList.size(); i++) {
Fragment multiplexing = multiplexingFragment.getMultiplexing(mList.get(i).getName(), "");
mFragmentList.add(multiplexing);
}
//添加fragment
for (int i = 0; i < mFragmentList.size(); i++) {
transaction.add(R.id.mFrame, mFragmentList.get(i));
}
transaction.commit();
}
/*
* 方法名:replese(int position)
* 功 能:根据点击事件的下标切换fragment页面
* 参 数:int position
* 返回值:无
*/
private void replese(int position) {
FragmentTransaction transaction = supportFragmentManager.beginTransaction();
//所有的fragment隐藏,position对应的fragment显示,提交。
for (int i = 0; i < mFragmentList.size(); i++) {
Fragment fragment = mFragmentList.get(i);
transaction.hide(fragment);
}
transaction.show(mFragmentList.get(position)).commit();
}
/*
* 方法名:onItemClick(AdapterView<?> parent, View view, int position, long id)
* 功 能:ListView的item点击事件
* 参 数:AdapterView<?> parent, View view, int position, long id
* 返回值:无
*/
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//切换fragment
replese(position);
//在bean类里写的一个标记 boolean类型的isSelect是关键,默认无状态, 并设置get set方法
//集合里所有数据的Select设置为flase,position下标所对应的item的Select为true,刷新适配器。
for (int i = 0; i <mList.size() ; i++) {
mList.get(i).setSelect(false);
}
mList.get(position).setSelect(true);
//在刷新一下适配器就ok
apader.notifyDataSetChanged();
Toast.makeText(MainActivity.this, "" + position, Toast.LENGTH_SHORT).show();
}
}
//第三步 创建User.java这个Bean类:
public class User {
private String name;
//isSelect用来在适配器进行item判断,并设置get set方法
private boolean isSelect;
public User(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isSelect() {
return isSelect;
}
public void setSelect(boolean select) {
isSelect = select;
}
}
//第四步 创建MyListViewApader适配器:
public class MyListViewApader extends BaseAdapter {
private List<User> mlist;
private Context context;
public MyListViewApader(List<User> mlist, Context context) {
this.mlist = mlist;
this.context = context;
}
@Override
public int getCount() {
return mlist.size();
}
@Override
public Object getItem(int position) {
return mlist.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@SuppressLint("ViewHolder")
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.item_layout, null);
holder.mListViewText = convertView.findViewById(R.id.mListViewText);
holder.mYueDouXianShi = convertView.findViewById(R.id.mYueDouXianShi);
holder.mYueDouLin = convertView.findViewById(R.id.mYueDouLin);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
String name = mlist.get(position).getName();
holder.mListViewText.setText(name);
//true和false用来解决复用问题的,如果当前标记为选中状态那么item字体颜色变红,红色竖条显示,item背景色变白
if (mlist.get(position).isSelect()) {
holder.mListViewText.setTextColor(Color.RED);
holder.mYueDouXianShi.setVisibility(View.VISIBLE);
holder.mYueDouLin.setBackgroundColor(Color.WHITE);
}
//如果不是选中状态,item字体颜色变黑,红色竖条隐藏,item背景还变灰
else {
holder.mListViewText.setTextColor(Color.BLACK);
holder.mYueDouXianShi.setVisibility(View.GONE);
holder.mYueDouLin.setBackgroundColor(Color.parseColor("#EBEBEB"));
}
return convertView;
}
public static class ViewHolder {
public TextView mListViewText;
public TextView mYueDouXianShi;
public LinearLayout mYueDouLin;
}
}
//第五步 MyListViewApader适配器的布局文件item_layout.xml:
<?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"
>
<LinearLayout
android:id="@+id/mYueDouLin"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="50dp">
<TextView
android:visibility="gone"
android:id="@+id/mYueDouXianShi"
android:background="@android:color/holo_red_dark"
android:layout_width="3dp"
android:layout_height="match_parent" />
<TextView
android:gravity="center"
android:id="@+id/mListViewText"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</LinearLayout>
//第六步 创建MultiplexingFragment这个fragment用来复用:
public class MultiplexingFragment extends Fragment {
private String name;
private String name2;
private TextView mText;
public MultiplexingFragment() {
// Required empty public constructor
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//------------在这里得到从下面的bundle里传来的两个string,并赋值给name和name2,并全局化.---------
Bundle arguments = getArguments();
if (arguments != null) {
name = arguments.getString("string");
name2 = arguments.getString("string2");
}
}
//复用的静态方法,两个参数都是String类型的,代表从Activity里传过来的一个集合的Name,一个数组的url地址
public static Fragment getMultiplexing(String string, String string2) {
MultiplexingFragment multiplexingFragment = new MultiplexingFragment();
Bundle bundle = new Bundle();
//得到并赋值给string和string2,通过bundle传递
bundle.putString("string", string);
bundle.putString("string2", string2);
multiplexingFragment.setArguments(bundle);
return multiplexingFragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View inflate = inflater.inflate(R.layout.fragment_multiplexing, container, false);
initView(inflate);
return inflate;
}
private void initView(View inflate) {
mText = (TextView) inflate.findViewById(R.id.mText);
mText.setText(name);
}
}
//第七步 MultiplexingFragment的布局文件fragment_multiplexing.xml:
<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="com.gannan.gannan.MultiplexingFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:id="@+id/mText"
android:textSize="20sp"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
//—————————————————————————–完—————————————————————————–
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/118363.html