Android 多商品訂單多圖評價功能實現(類似淘寶,可自定義可上傳圖片數量)

公司的商城類項目,需要對一份訂單的里面多個商品進行分別評價(圖片,文字內容,星級),為了方便使用抽時間實現了這個功能,整體來說還是比較簡單的。下面是ui效果圖

??

項目要求最多上傳5張圖片,這就涉及到換行的問題。最終找到了一個完美的結決方法。利用recycleView嵌套了一個流式布局

FlowTagLayout解決了動態添加復用的問題。

1、?主要布局文件?activity_comments.xml?


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:id="@+id/lv_comments"

android:layout_width="match_parent"

android:layout_height="match_parent">

1.1 、item的布局

item_comment.xml


android:orientation="vertical"

xmlns:app="http://schemas.android.com/apk/res-auto"

android:background="#ffffff"

android:layout_width="match_parent"

android:layout_height="wrap_content">

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:paddingLeft="15dp"

android:paddingTop="8dp"

android:paddingBottom="8dp"

android:orientation="horizontal">

android:id="@+id/iv_goods_image"

android:layout_width="40dp"

android:layout_height="40dp"

android:layout_gravity="center_vertical"

android:background="@mipmap/ic_default"/>

android:id="@+id/rat_comment"

style="?android:attr/ratingBarStyleSmall"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center_vertical"

android:layout_marginLeft="10dp"

android:isIndicator="false"

android:numStars="5"

android:progressTint="@color/yellow"

android:rating="0"

android:stepSize="0.1" />

android:layout_width="match_parent"

android:layout_height="1dp"

android:background="@color/line_bg"/>

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:paddingTop="15dp"

android:paddingBottom="15dp"

android:orientation="vertical">

android:id="@+id/et_comment"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:background="@null"

android:paddingLeft="15dp"

android:paddingRight="15dp"

android:hint="請填寫您的評價"

android:gravity="top"

android:textSize="14dp"

android:textCursorDrawable="@null"

android:textColorHint="#999999"

android:minLines="5"/>

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal">

android:id="@+id/fl_comment"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:focusable="true"

android:focusableInTouchMode="true"

android:layout_margin="10dp">

android:layout_width="match_parent"

android:layout_height="5dp"

android:background="@color/activity_gray"/>

1.2、流式布局的item文件tag_item2.xml


android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="#ffffff" >

android:id="@+id/iv_comment_image"

android:layout_width="70dp"

android:layout_height="70dp"

android:layout_marginTop="10dp"

android:layout_marginBottom="5dp"

android:layout_marginLeft="5dp"

android:layout_marginRight="5dp"

android:background="@mipmap/iv_up_image"/>

2、CommentsInfo 評論內容的model類

public class CommentsInfo {

private int ProductId;

private String CommentContent;

public List CommentImgs;

private double StarCount;

public int getProductId() {

return ProductId;

}

public void setProductId(int productId) {

ProductId = productId;

}

public String getCommentContent() {

return CommentContent;

}

public void setCommentContent(String commentContent) {

CommentContent = commentContent;

}

public List getCommentImgs() {

return CommentImgs;

}

public void setCommentImgs(List commentImgs) {

CommentImgs = commentImgs;

}

public double getStarCount() {

return StarCount;

}

public void setStarCount(double starCount) {

StarCount = starCount;

}

}

2.1、下面是用到的adapter

recycleView 的adapter類?CommentAdapter

public class CommentAdapter extends RecyclerView.Adapter {

private ArrayList list;

private Context context;

private OnStatusListener onStatusListener;

public CommentAdapter(ArrayList list, Context context) {

this.list = list;

this.context = context;

}

public void setOnStatusListener(OnStatusListener onStatusListener) {

this.onStatusListener = onStatusListener;

}

@Override

public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {

ViewHolder viewHolder = new ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_comment, viewGroup, false));

return viewHolder;

}

@Override

public void onBindViewHolder(ViewHolder viewHolder, final int i) {

viewHolder.flComment

.setTagCheckedMode(FlowTagLayout.FLOW_TAG_CHECKED_SINGLE);

TagAdapter1 mSizeTagAdapter1 = new TagAdapter1<>(context);

viewHolder.flComment.setAdapter(mSizeTagAdapter1);

mSizeTagAdapter1.onlyAddAll(list.get(i).getCommentImgs());

viewHolder.flComment.setOnTagSelectListener(new OnTagSelectListener() {

@Override

public void onItemSelect(FlowTagLayout parent, List selectedList) {

if(list.get(i).getCommentImgs().get(selectedList.get(0)).equals("")){

onStatusListener.onSetStatusListener(i);

}else{

onStatusListener.onDeleteListener(i,selectedList.get(0));

}

}

});

}

@Override

public long getItemId(int i) {

return i;

}

@Override

public int getItemCount() {

return list.size();

}

class ViewHolder extends RecyclerView.ViewHolder{

FlowTagLayout flComment;

public ViewHolder(View itemView) {

super(itemView);

flComment = itemView.findViewById(R.id.fl_comment);

}

}

public interface OnStatusListener {

void onSetStatusListener(int pos);

void onDeleteListener(int pos, int tagPos);

}

}

2.2、流式布局的adapter類?TagAdapter

public class TagAdapter extends BaseAdapter implements OnInitSelectedPosition {

private final Context mContext;

private final List mDataList;

public TagAdapter(Context context) {

this.mContext = context;

mDataList = new ArrayList();

}

@Override

public int getCount() {

return mDataList.size();

}

@Override

public Object getItem(int position) {

return mDataList.get(position);

}

@Override

public long getItemId(int position) {

return position;

}

@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)

@Override

public View getView(int position, View convertView, ViewGroup parent) {

View view = LayoutInflater.from(mContext).inflate(R.layout.tag_item2, null);

ImageView ivImage = (ImageView) view.findViewById(R.id.iv_comment_image);

T t = mDataList.get(position);

if (t instanceof String) {

if(t.equals("")){

ivImage.setBackground(mContext.getResources().getDrawable(R.mipmap.iv_up_image));

}else{

ImageLoader.getInstance().displayImage(t.toString(), ivImage);

}

}

return view;

}

public void onlyAddAll(List datas) {

mDataList.clear();

mDataList.addAll(datas);

notifyDataSetChanged();

}

public void clearAndAddAll(List datas) {

mDataList.clear();

onlyAddAll(datas);

}

@Override

public boolean isSelectedPosition(int position) {

if (position % 2 == 0) {

return true;

}

return false;

}

}

2.3、activity中的代碼

public class CommentsActivity extends Activity implements CommentAdapter.OnStatusListener{

ArrayList list = new ArrayList<>();

CommentAdapter commentAdapter;

RecyclerView lvComment;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_comments);

findViews();

}

protected void findViews() {

lvComment = findViewById(R.id.lv_comments);

LinearLayoutManager layoutmanager = new LinearLayoutManager(this);

//設置RecyclerView 布局

layoutmanager.setOrientation(LinearLayoutManager.VERTICAL);

lvComment.setLayoutManager(layoutmanager);

for(int i=0;i<3;i++){

CommentsInfo commentsInfo = new CommentsInfo();

List commentImgs = new ArrayList<>();

commentImgs.add("");

commentsInfo.setCommentImgs(commentImgs);

list.add(commentsInfo);

}

commentAdapter = new CommentAdapter(list,this);

commentAdapter.setOnStatusListener(this);

lvComment.setAdapter(commentAdapter);

}

//這里可以處理點擊添加圖片? 打開相冊或相機功能 然后將圖片的路徑存到對應的model中 刷新列表

@Override

public void onSetStatusListener(int pos) {

if(list.get(pos).getCommentImgs().size()<5){

CommentsInfo commentsInfo = list.get(pos);

List commentImgs = commentsInfo.getCommentImgs();

commentImgs.add(0,"http://img.xsmore.com/cjyp/Product/2ef24a07-f3b0-4497-8549-2acc0f7ca4b0.jpg");

commentsInfo.setCommentImgs(commentImgs);

list.set(pos,commentsInfo);

commentAdapter.notifyItemChanged(pos);

}else if(list.get(pos).getCommentImgs().size()==5){

CommentsInfo commentsInfo = list.get(pos);

List commentImgs = commentsInfo.getCommentImgs();

commentImgs.set(commentImgs.size()-1,"http://img.xsmore.com/cjyp/Product/2ef24a07-f3b0-4497-8549-2acc0f7ca4b0.jpg");

commentsInfo.setCommentImgs(commentImgs);

list.set(pos,commentsInfo);

commentAdapter.notifyItemChanged(pos);

}

}

@Override

public void onDeleteListener(int pos,int tagPos) {

CommentsInfo commentsInfo = list.get(pos);

List commentImgs = commentsInfo.getCommentImgs();

if(!commentImgs.get(commentImgs.size()-1).equals("")){

commentImgs.add("");

}

commentImgs.remove(tagPos);

commentsInfo.setCommentImgs(commentImgs);

list.set(pos,commentsInfo);

commentAdapter.notifyItemChanged(pos);

}

}

demo下載路徑點擊打開鏈接

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,119評論 6 531
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,382評論 3 415
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,038評論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,853評論 1 309
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,616評論 6 408
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,112評論 1 323
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,192評論 3 441
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,355評論 0 288
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,869評論 1 334
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,727評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,928評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,467評論 5 358
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,165評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,570評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,813評論 1 282
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,585評論 3 390
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,892評論 2 372

推薦閱讀更多精彩內容

  • 詳情頁面 packagecom.example.shoppingcar; importandroid.conten...
    ForCrazyLove閱讀 582評論 0 2
  • 本人初學Android,最近做了一個實現安卓簡單音樂播放功能的播放器,收獲不少,于是便記錄下來自己的思路與知識總結...
    落日柳風閱讀 19,184評論 2 41
  • 城市發展,日新月異,不破不立。很多基礎設施已功能落后,勞民傷財,如何緊跟時代步伐,更好的服務廣大人民群眾?擺在了城...
    古鎮老戴閱讀 701評論 0 0
  • 一路陽光匆匆過,一路團友共相伴。穿洲過省看城鄉,游客購買歡樂多。臨別依依說旅程,南來北往是過客。幾度夕陽落車后,那...
    甘朝武閱讀 211評論 0 0
  • RVM : Ruby Version ManagerRuby 是一種開源的面向對象程序設計的服務器端腳本語言Mac...
    間歇_持續閱讀 326評論 0 1