图片点击事件

master
bullfrog 3 years ago
parent 60f32b1980
commit a0c3c26d96

@ -4,7 +4,7 @@
<option name="filePathToZoomLevelMap"> <option name="filePathToZoomLevelMap">
<map> <map>
<entry key="..\:/Users/HP/Desktop/android_Course_setting/androidF/android_course_design/app/src/main/res/layout/goods_show_item.xml" value="0.25" /> <entry key="..\:/Users/HP/Desktop/android_Course_setting/androidF/android_course_design/app/src/main/res/layout/goods_show_item.xml" value="0.25" />
<entry key="..\:/Users/HP/Desktop/android_Course_setting/androidF/android_course_design/app/src/main/res/layout/item_stagger.xml" value="0.5" /> <entry key="..\:/Users/HP/Desktop/android_Course_setting/androidF/android_course_design/app/src/main/res/layout/item_stagger.xml" value="0.3729166666666667" />
<entry key="..\:/andriod/Android_Couser_Design/app/src/main/res/drawable/baseline_account_circle_18.xml" value="0.1345" /> <entry key="..\:/andriod/Android_Couser_Design/app/src/main/res/drawable/baseline_account_circle_18.xml" value="0.1345" />
<entry key="..\:/andriod/Android_Couser_Design/app/src/main/res/drawable/baseline_account_circle_20.xml" value="0.1345" /> <entry key="..\:/andriod/Android_Couser_Design/app/src/main/res/drawable/baseline_account_circle_20.xml" value="0.1345" />
<entry key="..\:/andriod/Android_Couser_Design/app/src/main/res/drawable/baseline_account_circle_24.xml" value="0.1345" /> <entry key="..\:/andriod/Android_Couser_Design/app/src/main/res/drawable/baseline_account_circle_24.xml" value="0.1345" />

@ -2,6 +2,7 @@ package com.android.activity.adapter;
import static android.content.ContentValues.TAG; import static android.content.ContentValues.TAG;
import android.annotation.SuppressLint;
import android.content.Context; import android.content.Context;
import android.util.Log; import android.util.Log;
import android.view.View; import android.view.View;
@ -29,6 +30,9 @@ public class StaggerAdapter extends RecyclerView.Adapter<StaggerAdapter.InnerHol
protected final ArrayList<Good> mData; protected final ArrayList<Good> mData;
private RecyclerView mList; private RecyclerView mList;
Context context; Context context;
private OnItemClickListener mOnItemClickListener;
public StaggerAdapter(ArrayList<Good> mData, Context context) { public StaggerAdapter(ArrayList<Good> mData, Context context) {
Log.d("TAG", "StaggerAdapter: "); Log.d("TAG", "StaggerAdapter: ");
@ -53,10 +57,15 @@ public class StaggerAdapter extends RecyclerView.Adapter<StaggerAdapter.InnerHol
//绑定holder一般用来设置数据 //绑定holder一般用来设置数据
@Override @Override
public void onBindViewHolder(@NonNull InnerHolder holder, int position) { public void onBindViewHolder(@NonNull InnerHolder holder, int position) {
Log.e("TAG", "onBindViewHolder-position"+position );
String strTvTitle; String strTvTitle;
String strTvCount; String strTvCount;
String imageUrl; String imageUrl;
holder.setmPosition(position);
if (mData.get(position) != null){ if (mData.get(position) != null){
strTvTitle = mData.get(position).getContent(); strTvTitle = mData.get(position).getContent();
@ -65,6 +74,8 @@ public class StaggerAdapter extends RecyclerView.Adapter<StaggerAdapter.InnerHol
Log.d("TAG", "Title:"+strTvTitle); Log.d("TAG", "Title:"+strTvTitle);
Log.d("TAG", "Count:"+strTvCount); Log.d("TAG", "Count:"+strTvCount);
Log.e("imageUrl", (String) mData.get(position).getImageUrlList().get(0)); Log.e("imageUrl", (String) mData.get(position).getImageUrlList().get(0));
holder.getTvTitle().setText(strTvTitle); holder.getTvTitle().setText(strTvTitle);
holder.getTvCount().setText(strTvCount); holder.getTvCount().setText(strTvCount);
@ -82,8 +93,21 @@ public class StaggerAdapter extends RecyclerView.Adapter<StaggerAdapter.InnerHol
return 0; return 0;
} }
//用于监听
public void setOnItemClickListener(OnItemClickListener listener) {
//设置一个监听,一个回调的接口
this.mOnItemClickListener = listener;
}
public interface OnItemClickListener{
//条目
void onItemClick(int position);
}
public class InnerHolder extends RecyclerView.ViewHolder { public class InnerHolder extends RecyclerView.ViewHolder {
private int mPosition;
private final ImageView mImage; private final ImageView mImage;
private final TextView mContent; private final TextView mContent;
private final TextView mPrice; private final TextView mPrice;
@ -96,24 +120,32 @@ public class StaggerAdapter extends RecyclerView.Adapter<StaggerAdapter.InnerHol
mContent = itemView.findViewById(R.id.tv_content); mContent = itemView.findViewById(R.id.tv_content);
mPrice = itemView.findViewById(R.id.tv_price); mPrice = itemView.findViewById(R.id.tv_price);
} itemView.setOnClickListener(new View.OnClickListener() {
public void setData(Good good) { @Override
//开始设置数据 public void onClick(View view) {
mContent.setText(good.getContent()); //监听
mPrice.setText(good.getPrice()); if (mOnItemClickListener != null) {
mOnItemClickListener.onItemClick(mPosition);
}
}
});
} }
public TextView getTvTitle() { public TextView getTvTitle() {
return mContent; return mContent;
} }
public TextView getTvCount() { public TextView getTvCount() {
return mPrice; return mPrice;
} }
public ImageView getIvGoods() { public ImageView getIvGoods() {
return mImage; return mImage;
} }
public void setmPosition(int position){
this.mPosition=position;
}
} }
// //实现瀑布流效果 // //实现瀑布流效果
// private void showStagger() { // private void showStagger() {

@ -3,20 +3,16 @@ package com.android.activity.fragment;
import android.os.Bundle; import android.os.Bundle;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager; import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import android.os.Parcelable;
import android.util.Log; import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.TextView; import android.widget.Toast;
import com.android.R; import com.android.R;
import com.android.activity.adapter.GoodsAdapter;
import com.android.activity.adapter.StaggerAdapter; import com.android.activity.adapter.StaggerAdapter;
import com.android.bean.Good; import com.android.bean.Good;
import com.android.utils.eventBus.EventMsg; import com.android.utils.eventBus.EventMsg;
@ -44,7 +40,7 @@ public class GoodsTypeFragment extends Fragment {
private EventMsg<Good> eventMsg; private EventMsg<Good> eventMsg;
private ArrayList<Good> typeGoodList; private ArrayList<Good> typeGoodList;
private RecyclerView recyclerView; private RecyclerView recyclerView;
private StaggerAdapter goodsAdapter; private StaggerAdapter staggerAdapter;
// private GoodsAdapter goodsAdapter; // private GoodsAdapter goodsAdapter;
View rootView; View rootView;
@ -101,7 +97,9 @@ public class GoodsTypeFragment extends Fragment {
recyclerView = rootView.findViewById(R.id.rl_goods_show); recyclerView = rootView.findViewById(R.id.rl_goods_show);
if (goodArrayList != null){ if (goodArrayList != null){
Log.d("TAG", "initView"); Log.d("TAG", "initView");
goodsAdapter = new StaggerAdapter(typeGoodList,getContext());
//创建适配器
staggerAdapter = new StaggerAdapter(typeGoodList,getContext());
// goodsAdapter = new GoodsAdapter(typeGoodList,getContext()); // goodsAdapter = new GoodsAdapter(typeGoodList,getContext());
// recyclerView.setLayoutManager(new LinearLayoutManager(getContext())); // recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
// recyclerView.setAdapter(goodsAdapter); // recyclerView.setAdapter(goodsAdapter);
@ -112,13 +110,25 @@ public class GoodsTypeFragment extends Fragment {
//设置布局管理器到RecyclerView中 //设置布局管理器到RecyclerView中
recyclerView.setLayoutManager(layoutManager); recyclerView.setLayoutManager(layoutManager);
//设置适配器 //设置适配器
recyclerView.setAdapter(goodsAdapter); recyclerView.setAdapter(staggerAdapter);
//设置监听事件
initListener();
} }
// TextView view = rootView.findViewById(R.id.tv_goods); // TextView view = rootView.findViewById(R.id.tv_goods);
// view.setText(goodArrayList.get(1).getUsername()); // view.setText(goodArrayList.get(1).getUsername());
} }
private void initListener() {
staggerAdapter.setOnItemClickListener(new StaggerAdapter.OnItemClickListener() {
@Override
public void onItemClick(int position) {
//监听的各种操作写在这里
Toast.makeText(getActivity(), "我点击了第"+position+"item", Toast.LENGTH_SHORT).show();
}
});
}
@Override @Override
public void onStart() { public void onStart() {

@ -17,13 +17,13 @@ import retrofit2.http.QueryMap;
public interface GoodsHttp_interface { public interface GoodsHttp_interface {
@GET("tran/goods/type") @GET("tran/goods/type")
@Headers({"appId:e843562fefa144bf808a9621b107b3a4", @Headers({"appId:b34ac21286ae45938add627b418a4871",
"appSecret:76387a99d8b52fad54b94bea1118262573381"}) "appSecret:67526def9de11d4a64f5e80e60ed3372eea69"})
Observable<ResponseData<List<GoodsType>>> sendGetAllGoodsType(); Observable<ResponseData<List<GoodsType>>> sendGetAllGoodsType();
@GET("tran/goods/all") @GET("tran/goods/all")
@Headers({"appId:e843562fefa144bf808a9621b107b3a4", @Headers({"appId:b34ac21286ae45938add627b418a4871",
"appSecret:76387a99d8b52fad54b94bea1118262573381"}) "appSecret:67526def9de11d4a64f5e80e60ed3372eea69"})
Observable<ResponseData<GoodRecords>> sendGetAllGood(@Query("userId") Integer userId, Observable<ResponseData<GoodRecords>> sendGetAllGood(@Query("userId") Integer userId,
@QueryMap Map<String, Object> map @QueryMap Map<String, Object> map
); );

@ -78,7 +78,7 @@ public class GoodsModelimpl implements GoodsModel{
Map<String, Object> map = new ArrayMap<>(); Map<String, Object> map = new ArrayMap<>();
Observable<ResponseData<GoodRecords>> observable Observable<ResponseData<GoodRecords>> observable
= httpInterface.sendGetAllGood(17, map); = httpInterface.sendGetAllGood(6, map);
observable.subscribeOn(Schedulers.io()) observable.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())

@ -2,60 +2,73 @@
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:background="@color/grey"> xmlns:app="http://schemas.android.com/apk/res-auto">
<RelativeLayout <androidx.cardview.widget.CardView
android:layout_width="200dp" android:layout_width="match_parent"
android:layout_height="280dp" android:layout_height="match_parent"
android:layout_marginStart="15dp" app:cardUseCompatPadding="true"
android:layout_marginTop="15dp" app:cardCornerRadius="10dp"
android:layout_marginEnd="15dp" >
android:layout_marginBottom="15dp"
android:background="@color/white"> <RelativeLayout
<ImageView
android:id="@+id/iv_image"
android:layout_width="200dp" android:layout_width="200dp"
android:layout_height="200dp" android:layout_height="280dp"
android:scaleType="fitXY" /> android:layout_marginStart="15dp"
android:layout_marginTop="15dp"
<TextView android:layout_marginEnd="15dp"
android:id="@+id/tv_content"
android:layout_width="180dp"
android:layout_height="25dp"
android:layout_below="@+id/iv_image"
android:layout_marginStart="13dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="15dp" android:layout_marginBottom="15dp"
android:textSize="20dp" /> android:background="@color/white">
<TextView
android:id="@+id/str" <ImageView
android:layout_width="wrap_content" android:id="@+id/iv_image"
android:layout_height="wrap_content" android:layout_width="200dp"
android:layout_below="@+id/tv_content" android:layout_height="200dp"
android:layout_alignLeft="@+id/tv_content" android:scaleType="fitXY" />
android:text="价格: "
android:textColor="@color/red" />
<TextView
android:id="@+id/tv_price" <TextView
android:layout_width="100dp" android:id="@+id/tv_content"
android:layout_height="18dp" android:layout_width="180dp"
android:layout_below="@+id/tv_content" android:layout_height="25dp"
android:layout_toRightOf="@+id/str" android:layout_below="@+id/iv_image"
android:textColor="@color/red" /> android:layout_marginTop="10dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="15dp"
<!-- <TextView--> android:textSize="20dp" />
<!-- android:id="@+id/iv_delete"-->
<!-- android:layout_width="30dp"--> <TextView
<!-- android:layout_height="30dp"--> android:id="@+id/str"
<!-- android:layout_alignEnd="@+id/tv_content"--> android:layout_width="wrap_content"
<!-- android:layout_alignBottom="@+id/tv_price"--> android:layout_height="wrap_content"
<!-- android:src="@drawable/ic_baseline_close_24" />--> android:layout_below="@+id/tv_content"
android:layout_alignLeft="@+id/tv_content"
</RelativeLayout> android:text="@string/str"
android:textColor="@color/red" />
<TextView
android:id="@+id/tv_price"
android:layout_width="100dp"
android:layout_height="18dp"
android:layout_below="@+id/tv_content"
android:layout_toRightOf="@+id/str"
android:textColor="@color/red" />
<!-- <TextView-->
<!-- android:id="@+id/iv_delete"-->
<!-- android:layout_width="30dp"-->
<!-- android:layout_height="30dp"-->
<!-- android:layout_alignEnd="@+id/tv_content"-->
<!-- android:layout_alignBottom="@+id/tv_price"-->
<!-- android:src="@drawable/ic_baseline_close_24" />-->
</RelativeLayout>
</androidx.cardview.widget.CardView>
</RelativeLayout> </RelativeLayout>

@ -2,5 +2,6 @@
<string name="app_name">Android_Couser_Design</string> <string name="app_name">Android_Couser_Design</string>
<!-- TODO: Remove or change this placeholder text --> <!-- TODO: Remove or change this placeholder text -->
<string name="hello_blank_fragment">Hello blank fragment</string> <string name="hello_blank_fragment">Hello blank fragment</string>
<string name="str"></string>
</resources> </resources>
Loading…
Cancel
Save