二阶段团队冲刺(5)
今日新闻功能
答题功能
package com.llw.goodt; import androidx.appcompat.app.AppCompatActivity; import androidx.fragment.app.Fragment; import androidx.viewpager.widget.ViewPager; import android.annotation.SuppressLint; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; import com.google.android.material.bottomsheet.BottomSheetDialog; import com.llw.goodt.Fragment.DaTiFragment; import com.llw.goodt.Util.QuestionUtils; import com.llw.goodt.adapter.DaTiAdapter; import com.llw.goodt.adapter.ImageAdapter; import java.util.ArrayList; import java.util.List; //#428E46浠h〃缁胯壊锛屽洖绛旀纭?//#DC2A1D浠h〃绾㈣壊锛屽洖绛旈敊璇?public class QuestionActivity extends AppCompatActivity { private static final String TAG = "btn_id"; private LinearLayout ll_a; private LinearLayout lL_b; private LinearLayout ll_c; private LinearLayout ll_d; private Boolean[] abcd = new Boolean[]{false,false,false,false}; private ImageView iv_a; private ImageView iv_b; private ImageView iv_c; private ImageView iv_d; private ViewPager vp; private List<Fragment> myFragmentList = new ArrayList<>(); private TextView tv_num; private Button btn_tika; private List<Button> myBtnList = new ArrayList<>(); private ImageView iv_back; @SuppressLint("MissingInflatedId") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_question); initView(); DaTiAdapter adapter = new DaTiAdapter(getSupportFragmentManager(),myFragmentList); vp.setAdapter(adapter); vp.setCurrentItem(0); vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @SuppressLint("SetTextI18n") @Override public void onPageSelected(int position) { int shu = 0; String user_input = QuestionUtils.user_input[position]; if(user_input.equals("A")) shu = 1; if(user_input.equals("B")) shu = 2; if(user_input.equals("C")) shu = 3; if(user_input.equals("D")) shu = 4; DaTiFragment fragment = (DaTiFragment) myFragmentList.get(position); fragment.setXuanXiang(shu);//璁剧疆閫夋嫨鐨勯€夐」 tv_num.setText(position+1+""); //浠ヤ笅鐨勪唬鐮佺敤浜庤缃炕瀹岄〉涔嬪悗鏍规嵁璁板綍鐨勭瓟妗堟潵鍒ゆ柇鏄洖绛旀纭繕鏄敊璇? //濡傛灉缈诲畬椤典箣鍚庯紝 if(QuestionUtils.user_jiexi[position]){ fragment.setTextOpen(); } } @Override public void onPageScrollStateChanged(int state) { } }); } private void initView() { iv_back = findViewById(R.id.iv_back); iv_back.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { finish(); } }); btn_tika = findViewById(R.id.btn_tika); //鐐瑰嚮棰樺崱锛屽脊鍑哄簳閮ㄥ璇濇 btn_tika.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(QuestionActivity.this); // 璁剧疆搴曢儴瀵硅瘽妗嗙殑甯冨眬鏂囦欢锛屾垜浠彲浠ョ洿鎺ラ€氳繃dialog鏉ヨ幏鍙栧埌涓嶅潎鏂囦欢鎵€瀵瑰簲鐨勬帶浠? bottomSheetDialog.setContentView(R.layout.bottom_sheet_dialog_layout); Button btn_1 = bottomSheetDialog.findViewById(R.id.btn_1); myBtnList.add(btn_1);Button btn_2 = bottomSheetDialog.findViewById(R.id.btn_2);myBtnList.add(btn_2); Button btn_3 = bottomSheetDialog.findViewById(R.id.btn_3); myBtnList.add(btn_3);Button btn_4 = bottomSheetDialog.findViewById(R.id.btn_4);myBtnList.add(btn_4); Button btn_5 = bottomSheetDialog.findViewById(R.id.btn_5); myBtnList.add(btn_5);Button btn_6 = bottomSheetDialog.findViewById(R.id.btn_6);myBtnList.add(btn_6); Button btn_7 = bottomSheetDialog.findViewById(R.id.btn_7); myBtnList.add(btn_7);Button btn_8 = bottomSheetDialog.findViewById(R.id.btn_8);myBtnList.add(btn_8); Button btn_9 = bottomSheetDialog.findViewById(R.id.btn_9); myBtnList.add(btn_9);Button btn_10 =bottomSheetDialog.findViewById(R.id.btn_10); myBtnList.add(btn_10); for (int i = 0; i < 10; i++) { int finalI = i; Button button = myBtnList.get(i); if(!QuestionUtils.user_input[i].equals("F")){ button.setBackgroundResource(R.drawable.btn_bg); } myBtnList.get(i).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { vp.setCurrentItem(finalI);bottomSheetDialog.dismiss(); } }); } // 鏄剧ず搴曢儴瀵硅瘽妗? bottomSheetDialog.show(); myBtnList.clear(); } }); tv_num = findViewById(R.id.tv_num); for (int i = 0; i < 10; i++) { ArrayList<String> xuanxiang = new ArrayList<>(); xuanxiang.add(QuestionUtils.xx_a[i]); xuanxiang.add(QuestionUtils.xx_b[i]); xuanxiang.add(QuestionUtils.xx_c[i]); xuanxiang.add(QuestionUtils.xx_d[i]); DaTiFragment fragment = DaTiFragment.newInstance(QuestionUtils.timu[i],xuanxiang,i+1,QuestionUtils.jiexiti[i]); myFragmentList.add(fragment); } vp = findViewById(R.id.vp); // ll_a = findViewById(R.id.ll_a); ll_a.setOnClickListener(this); // lL_b = findViewById(R.id.ll_b); lL_b.setOnClickListener(this); // ll_c = findViewById(R.id.ll_c); ll_c.setOnClickListener(this); // ll_d = findViewById(R.id.ll_d); ll_d.setOnClickListener(this); iv_a = findViewById(R.id.iv_a); iv_b = findViewById(R.id.iv_b); iv_c = findViewById(R.id.iv_c); iv_d = findViewById(R.id.iv_d); } // @Override // public void onClick(View v) { // for (int i = 0; i < 4; i++) { // abcd[i] = false; // } // iv_a.setImageResource(R.drawable.a); // iv_b.setImageResource(R.drawable.b); // iv_c.setImageResource(R.drawable.c); // iv_d.setImageResource(R.drawable.d); // // switch (v.getId()){ // case R.id.ll_a: //// 濡傛灉鏈閫変腑 // iv_a.setImageResource(R.drawable.a1);break; // case R.id.ll_b: //// 濡傛灉鏈閫変腑 // iv_b.setImageResource(R.drawable.b1);break; // case R.id.ll_c: //// 濡傛灉鏈閫変腑 // iv_c.setImageResource(R.drawable.c1);break; // case R.id.ll_d: //// 濡傛灉鏈閫変腑 // iv_d.setImageResource(R.drawable.d1);break; // } // } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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=".QuestionActivity" android:orientation="vertical"> <androidx.appcompat.widget.Toolbar android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#FFFFFF" android:id="@+id/tool_bar"> <ImageView android:id="@+id/iv_back" android:layout_width="30dp" android:layout_height="30dp" android:src="@drawable/ic_back" android:layout_gravity="center_vertical"/> <TextView android:id="@+id/tv_title" android:layout_marginStart="145dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="17sp" android:textColor="@color/black" android:text="姣忔棩绛旈"/> </androidx.appcompat.widget.Toolbar> <!-- 棰樼洰绫诲瀷--> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:layout_marginLeft="40dp" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="鍗曢€夐" android:textColor="@color/black" /> <TextView android:id="@+id/tv_num" android:layout_marginLeft="250dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20sp" android:textColor="#F39203" android:text="1"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="/10"/> </LinearLayout> <androidx.viewpager.widget.ViewPager android:layout_width="match_parent" android:layout_height="650dp" android:id="@+id/vp"> </androidx.viewpager.widget.ViewPager> <Button android:layout_marginLeft="20dp" android:background="@drawable/btn_bg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btn_tika" android:text="棰?鍗? android:textSize="20sp" android:textColor="@color/white" /> </LinearLayout>
新闻功能
<?xml version="1.0" encoding="utf-8"?><!--协调布局--> <androidx.coordinatorlayout.widget.CoordinatorLayout 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" android:background="@color/line_color" android:fitsSystemWindows="true" tools:context=".MainActivity"> <!--应用栏布局--> <com.google.android.material.appbar.AppBarLayout android:id="@+id/appbar_layout" android:layout_width="match_parent" android:layout_height="@dimen/dp_240" android:fitsSystemWindows="true" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> <!--折叠工具栏布局--> <com.google.android.material.appbar.CollapsingToolbarLayout android:id="@+id/toolbar_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" app:collapsedTitleGravity="center_horizontal" app:contentScrim="@color/colorPrimaryDark" app:layout_scrollFlags="scroll|exitUntilCollapsed" app:title="垃圾分类" app:toolbarId="@+id/toolbar"> <!--轮播--> <com.youth.banner.Banner android:id="@+id/banner" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/default_banner" android:fitsSystemWindows="true" app:layout_collapseMode="parallax" app:layout_collapseParallaxMultiplier="1" /> <androidx.appcompat.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" app:contentInsetEnd="0dp" app:contentInsetStart="0dp" app:layout_collapseMode="pin" app:layout_scrollFlags="scroll|snap" app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /> </com.google.android.material.appbar.CollapsingToolbarLayout> </com.google.android.material.appbar.AppBarLayout> <!--嵌套滑动布局--> <androidx.core.widget.NestedScrollView android:layout_width="match_parent" android:layout_height="match_parent" android:fillViewport="true" android:orientation="vertical" android:overScrollMode="never" app:layout_behavior="@string/appbar_scrolling_view_behavior"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <!--垃圾分类新闻列表--> <androidx.recyclerview.widget.RecyclerView android:id="@+id/rv_news" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="@dimen/dp_2" android:overScrollMode="never" /> </LinearLayout> </androidx.core.widget.NestedScrollView> </androidx.coordinatorlayout.widget.CoordinatorLayout>
首先要完成这个列表数据的显示,这里还是会有item的布局和适配器,首先创建item的布局,在layout下新建一个item_trash_new_rv.xml。里面的代码如下: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="@dimen/dp_1" android:id="@+id/item_trash_news" android:foreground="?attr/selectableItemBackground" android:background="@color/white" android:orientation="vertical" android:padding="@dimen/dp_16"> <!--标题--> <TextView android:id="@+id/tv_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ellipsize="end" android:singleLine="true" android:text="标题" android:textColor="@color/black" android:textSize="@dimen/sp_16" android:textStyle="bold" /> <!--内容描述--> <TextView android:id="@+id/tv_description" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/tv_title" android:layout_marginTop="@dimen/dp_8" android:text="内容描述" android:textColor="@color/black" android:textSize="@dimen/sp_14" /> </RelativeLayout>
然后就是适配器,在adapter下新建一个TrashNewsAdapter类,里面的代码如下: package com.llw.goodtrash.adapter; import androidx.annotation.Nullable; import com.chad.library.adapter.base.BaseQuickAdapter; import com.chad.library.adapter.base.BaseViewHolder; import com.llw.goodtrash.R; import com.llw.goodtrash.model.TrashNewsResponse; import java.util.List; /** * 垃圾分类新闻列表适配器 * */ public class TrashNewsAdapter extends BaseQuickAdapter<TrashNewsResponse.NewslistBean, BaseViewHolder> { public TrashNewsAdapter(int layoutResId, @Nullable List<TrashNewsResponse.NewslistBean> data) { super(layoutResId, data); } @Override protected void convert(BaseViewHolder helper, TrashNewsResponse.NewslistBean item) { helper.setText(R.id.tv_title, item.getTitle()) .setText(R.id.tv_description, item.getDescription()) .addOnClickListener(R.id.item_trash_news); } }
/** * 页面初始化 */ private void initView() { banner = findViewById(R.id.banner); collapsingToolbarLayout = findViewById(R.id.toolbar_layout); appBarLayout = findViewById(R.id.appbar_layout); rvNews = findViewById(R.id.rv_news); //伸缩偏移量监听 appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() { boolean isShow = true; int scrollRange = -1; @Override public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) { if (scrollRange == -1) { scrollRange = appBarLayout.getTotalScrollRange(); } if (scrollRange + verticalOffset == 0) {//收缩时 collapsingToolbarLayout.setTitle("垃圾分类"); isShow = true; } else if (isShow) {//展开时 collapsingToolbarLayout.setTitle(""); isShow = false; } } }); //设置列表 mAdapter = new TrashNewsAdapter(R.layout.item_trash_new_rv, mList); mAdapter.setOnItemChildClickListener((adapter, view, position) -> { showMsg("点击了第" + position + "个"); }); rvNews.setLayoutManager(new LinearLayoutManager(context)); rvNews.setAdapter(mAdapter); //请求垃圾分类新闻数据 mPresenter.getTrashNews(10); }
今日总结,答题功能开始,新闻功能初步完成
明天继续开展。