-
-
Notifications
You must be signed in to change notification settings - Fork 165
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
676 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 134 additions & 0 deletions
134
app/src/main/java/com/cretin/www/expandabletextview/ShowInRecyclerViewActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
package com.cretin.www.expandabletextview; | ||
|
||
import android.content.Context; | ||
import android.support.design.widget.FloatingActionButton; | ||
import android.support.v7.app.AppCompatActivity; | ||
import android.os.Bundle; | ||
import android.support.v7.widget.DividerItemDecoration; | ||
import android.support.v7.widget.LinearLayoutManager; | ||
import android.support.v7.widget.RecyclerView; | ||
import android.view.LayoutInflater; | ||
import android.view.View; | ||
import android.view.ViewGroup; | ||
import android.widget.Toast; | ||
|
||
import com.cretin.www.expandabletextview.model.ViewModel; | ||
import com.cretin.www.expandabletextview.model.ViewModelWithFlag; | ||
import com.ctetin.expandabletextviewlibrary.ExpandableTextView; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class ShowInRecyclerViewActivity extends AppCompatActivity { | ||
private RecyclerView recyclerView; | ||
private MyRecyclerViewAdapter adapter; | ||
private List list; | ||
|
||
private FloatingActionButton floatingActionButton; | ||
|
||
//是需要保存展开或收回状态 | ||
private boolean flag; | ||
|
||
private String yourText = " 我所认识的中国,强大、友好。@奥特曼 “一带一路”经济带带动了沿线国家的经济发展,促进我国与他国的友好往来和贸易发展,可谓“双赢”。http://www.baidu.com 自古以来,中国以和平、友好的面孔示人。汉武帝派张骞出使西域,开辟丝绸之路,增进与西域各国的友好往来。http://www.baidu.com 胡麻、胡豆、香料等食材也随之传入中国,汇集于中华美食。@RNG 漠漠古道,驼铃阵阵,这条路奠定了“一带一路”的基础,让世界认识了中国。"; | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_show_in_recycler_view); | ||
|
||
recyclerView = findViewById(R.id.recyclerview); | ||
floatingActionButton = findViewById(R.id.float_btn); | ||
|
||
list = new ArrayList<>(); | ||
|
||
for (int i = 0; i < 50; i++) { | ||
list.add(new ViewModel(yourText)); | ||
} | ||
|
||
adapter = new MyRecyclerViewAdapter(this, list); | ||
recyclerView.setLayoutManager(new LinearLayoutManager(this)); | ||
recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL)); | ||
recyclerView.setAdapter(adapter); | ||
|
||
floatingActionButton.setOnClickListener(new View.OnClickListener() { | ||
@Override | ||
public void onClick(View v) { | ||
changeStateAndSetData(); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* 切换状态 | ||
*/ | ||
private void changeStateAndSetData() { | ||
flag = !flag; | ||
list.clear(); | ||
if (flag) { | ||
Toast.makeText(this, "保留之前的展开或收回状态", Toast.LENGTH_SHORT).show(); | ||
floatingActionButton.setImageResource(R.mipmap.green); | ||
for (int i = 0; i < 50; i++) { | ||
list.add(new ViewModelWithFlag(yourText)); | ||
} | ||
} else { | ||
Toast.makeText(this, "不保留之前的展开或收回状态", Toast.LENGTH_SHORT).show(); | ||
floatingActionButton.setImageResource(R.mipmap.gray); | ||
for (int i = 0; i < 50; i++) { | ||
list.add(new ViewModel(yourText)); | ||
} | ||
} | ||
adapter.notifyDataSetChanged(); | ||
} | ||
|
||
|
||
public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.RecyclerHolder> { | ||
private Context mContext; | ||
private List dataList; | ||
|
||
public MyRecyclerViewAdapter(Context context, List list) { | ||
this.mContext = context; | ||
this.dataList = list; | ||
} | ||
|
||
@Override | ||
public RecyclerHolder onCreateViewHolder(ViewGroup parent, int viewType) { | ||
View view = LayoutInflater.from(mContext).inflate(R.layout.item_text, parent, false); | ||
return new RecyclerHolder(view); | ||
} | ||
|
||
@Override | ||
public void onBindViewHolder(RecyclerHolder holder, int position) { | ||
if (flag) { | ||
//注意:保留状态需要在设置内容之前调用bind方法 | ||
holder.textView.bind((ViewModelWithFlag) list.get(position)); | ||
holder.textView.setContent(((ViewModelWithFlag) list.get(position)).getTitle()); | ||
} else { | ||
holder.textView.setContent(((ViewModel) list.get(position)).getTitle()); | ||
} | ||
holder.textView.setLinkClickListener((type, content1) -> { | ||
switch (type) { | ||
case LINK_TYPE: | ||
Toast.makeText(mContext, "点击链接:" + content1, Toast.LENGTH_SHORT).show(); | ||
break; | ||
case MENTION_TYPE: | ||
Toast.makeText(mContext, "点击用户:" + content1, Toast.LENGTH_SHORT).show(); | ||
break; | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public int getItemCount() { | ||
return dataList.size(); | ||
} | ||
|
||
class RecyclerHolder extends RecyclerView.ViewHolder { | ||
private ExpandableTextView textView; | ||
|
||
private RecyclerHolder(View itemView) { | ||
super(itemView); | ||
textView = itemView.findViewById(R.id.tv_item); | ||
} | ||
} | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
app/src/main/java/com/cretin/www/expandabletextview/model/ViewModel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.cretin.www.expandabletextview.model; | ||
|
||
/** | ||
* @date: on 2018/9/20 | ||
* @author: cretin | ||
* @email: [email protected] | ||
* @desc: ViewModel 不需要记住之前的状态 | ||
*/ | ||
public class ViewModel { | ||
private String title; | ||
|
||
public ViewModel(String title) { | ||
this.title = title; | ||
} | ||
|
||
public String getTitle() { | ||
return title; | ||
} | ||
|
||
public void setTitle(String title) { | ||
this.title = title; | ||
} | ||
} |
Oops, something went wrong.