图片点击事件

master
bullfrog 3 years ago
parent 60f32b1980
commit a0c3c26d96

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

@ -3,20 +3,16 @@ package com.android.activity.fragment;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import android.os.Parcelable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import com.android.R;
import com.android.activity.adapter.GoodsAdapter;
import com.android.activity.adapter.StaggerAdapter;
import com.android.bean.Good;
import com.android.utils.eventBus.EventMsg;
@ -44,7 +40,7 @@ public class GoodsTypeFragment extends Fragment {
private EventMsg<Good> eventMsg;
private ArrayList<Good> typeGoodList;
private RecyclerView recyclerView;
private StaggerAdapter goodsAdapter;
private StaggerAdapter staggerAdapter;
// private GoodsAdapter goodsAdapter;
View rootView;
@ -101,7 +97,9 @@ public class GoodsTypeFragment extends Fragment {
recyclerView = rootView.findViewById(R.id.rl_goods_show);
if (goodArrayList != null){
Log.d("TAG", "initView");
goodsAdapter = new StaggerAdapter(typeGoodList,getContext());
//创建适配器
staggerAdapter = new StaggerAdapter(typeGoodList,getContext());
// goodsAdapter = new GoodsAdapter(typeGoodList,getContext());
// recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
// recyclerView.setAdapter(goodsAdapter);
@ -112,13 +110,25 @@ public class GoodsTypeFragment extends Fragment {
//设置布局管理器到RecyclerView中
recyclerView.setLayoutManager(layoutManager);
//设置适配器
recyclerView.setAdapter(goodsAdapter);
recyclerView.setAdapter(staggerAdapter);
//设置监听事件
initListener();
}
// TextView view = rootView.findViewById(R.id.tv_goods);
// 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
public void onStart() {

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

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

@ -2,60 +2,73 @@
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/grey">
<RelativeLayout
android:layout_width="200dp"
android:layout_height="280dp"
android:layout_marginStart="15dp"
android:layout_marginTop="15dp"
android:layout_marginEnd="15dp"
android:layout_marginBottom="15dp"
android:background="@color/white">
<ImageView
android:id="@+id/iv_image"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:cardUseCompatPadding="true"
app:cardCornerRadius="10dp"
>
<RelativeLayout
android:layout_width="200dp"
android:layout_height="200dp"
android:scaleType="fitXY" />
<TextView
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_height="280dp"
android:layout_marginStart="15dp"
android:layout_marginTop="15dp"
android:layout_marginEnd="15dp"
android:layout_marginBottom="15dp"
android:textSize="20dp" />
<TextView
android:id="@+id/str"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tv_content"
android:layout_alignLeft="@+id/tv_content"
android:text="价格: "
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>
android:background="@color/white">
<ImageView
android:id="@+id/iv_image"
android:layout_width="200dp"
android:layout_height="200dp"
android:scaleType="fitXY" />
<TextView
android:id="@+id/tv_content"
android:layout_width="180dp"
android:layout_height="25dp"
android:layout_below="@+id/iv_image"
android:layout_marginTop="10dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="15dp"
android:textSize="20dp" />
<TextView
android:id="@+id/str"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tv_content"
android:layout_alignLeft="@+id/tv_content"
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>

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