使用当前日期和时间的RecyclerView动态部分标题 [英] Dynamic Section Header Of RecyclerView Using Current Date&Time
问题描述
我将使用RecyclerView作为节标题
我想在插入每个日期和时间时创建节标题SQLite数据库中的时间数据
我在下面的链接中找到了这个解决方案,但没有成功
-
上面的图像数据使用下面的代码或部分是静态的:
列表<公司> childList = new ArrayList<>();
列表< CompanySectionHeader> sectionHeaders = new ArrayList<>();
childList = myDb.getAllCompany();
sectionHeaders.add(new CompanySectionHeader(childList,WEDNESDAY 4 APRIL,1));
现在如果我输入今天的数据,那么我创建1个截面日期的日期
图片数据部分下方标题仍为静态或数据:
以上图片数据正在使用以下代码:
childList.add(新公司(Ketul Inc.,11/11/2017 3:46 PM));
childList.add(新公司(Atmel Corporation,09/19/2017 8:46 PM));
childList.add(新公司(ABC Technologies,09/12/2017 7:41 PM));
childList.add(新公司(Huron Capital Partners LLC,09/12/2017 7:25 PM));
sectionHeaders = new ArrayList<>();
//创建SectionHeader列表DataModel实现SectionHeader
sectionHeaders.add(new CompanySectionHeader(childList,SATURDAY 7 APRIL,2));
以下代码是我的
SectionHeader.Java
:公共类CompanySectionHeader实现Section< Company>,Comparable< CompanySectionHeader> {
List< Company>的childList;
String sectionText;
int index;
public CompanySectionHeader(List< Company> childList,String sectionText,int index){
this.childList = childList;
this.sectionText = sectionText;
this.index = index;
}
@Override
public List< Company> getChildItems(){
return childList;
}
public String getSectionText(){
return sectionText;
}
@Override
public int compareTo(CompanySectionHeader another){
if(this.index> another.index){
return -1 ;
}其他{
返回1;
}
}
}
以下是我的SQLite数据库结构:
public String getFromatDate(long dateTime){
String formatedDate;
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(dateTime);
日期mDate = calendar.getTime();
SimpleDateFormat sdf;
sdf = new SimpleDateFormat(dd / MM / yyyy hh:mm a,新的Locale(en));
formatedDate = sdf.format(mDate);
return formatedDate;
}
public long insertCompany(公司公司){
// String sql = null;
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(DatabaseHelper.KEY_COMPANY_NAME,company.getName());
values.put(DatabaseHelper.KEY_CREATED_AT,System.currentTimeMillis());
values.put(DatabaseHelper.KEY_UPDATED_AT,System.currentTimeMillis());
values.put(DatabaseHelper.KEY_COMPANY_WEBSITE,company.getWebsite());
values.put(DatabaseHelper.KEY_COMPANY_EMAIL,company.getEmail());
values.put(DatabaseHelper.KEY_COMPANY_PHONE_HOME,company.getPhoneHome());
long company_id = db.insert(COMPANY,null,values);
return company_id;
}
我的问题是如何动态创建章节标题
如果您需要任何代码或信息,可以问我:)
在此先感谢:
解决方案我最近在每月基础上对此进行了排序。它通过覆盖ViewType函数。
< img src =https://i.stack.imgur.com/XbiVO.pngalt =屏幕截图>
为此,你有在Recycler Adapter中使用类似
getViewType()
的内容。公共类LastTransAdapter扩展了RecyclerView.Adapter< LastTransAdapter.ViewHolder> {
private ArrayList< LastTransBean> lastTransBeans;
私有上下文上下文;
private MyCustomTextView textViewTitle,tv_Desc,tv_Date,tv_Amount;
private LinearLayout layout1;
private查看customView,myView;
private LayoutParams layoutParams;
private PopupWindow popUpWindow = null;
私有RelativeLayout布局;
private int MONTHHEADER = 1;
private int DATAVIEW = 2;
public LastTransAdapter(ArrayList< LastTransBean> lastTransBeans,Context context,View myView,RelativeLayout layout){
this.lastTransBeans = lastTransBeans;
this.context = context;
this.myView = myView;
this.layout = layout;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup,int i){
if(i == DATAVIEW){
//查看正常数据。
查看视图= LayoutInflater.from(viewGroup.getContext())。inflate(R.layout.single_row_last_transactions,viewGroup,false);
返回新的ViewHolder(查看);
} else {
//月份或日期标题的查看类型
查看视图= LayoutInflater.from(viewGroup.getContext())。inflate(R。 layout.single_row_month_header,viewGroup,false);
返回新的ViewHolder(查看);
}
}
@Override
public void onBindViewHolder(ViewHolder viewHolder,int i){
final int position = i;
if(viewHolder.getItemViewType()== DATAVIEW){
//填充普通视图的数据
} else {
//设置你的日期或月份标题
}
}
@Override
public int getItemCount(){
return lastTransBeans.size();
}
@Override
public int getItemViewType(int position){
if(lastTransBeans.get(position).getMonth()){
return MONTHHEADER;
}其他{
返回DATAVIEW;
}
}
公共类ViewHolder扩展了RecyclerView.ViewHolder {
MyCustomTextView transtile,transdate,transamont,tv_monthHeader;
LinearLayout acctype;
CardView mlastTransCardView;
public ViewHolder(查看itemView){
super(itemView);
//投射所有文本视图,buttonsetc使用离子视图Holder。
}
}
}
你可以还使用多个视图类型。 喜欢此示例。
希望这可以帮助您解决问题
I'll use RecyclerView for a section header
I want to create section header when i insert each Date & Time data in SQLite Database
I followed below links for this solution but haven't been successful
Please refer the below image
For above image data using below code OR Section is Static :
List<Company> childList = new ArrayList<>(); List<CompanySectionHeader> sectionHeaders = new ArrayList<>(); childList = myDb.getAllCompany(); sectionHeaders.add(new CompanySectionHeader(childList, "WEDNESDAY 4 APRIL", 1));
Now If suppose i enter today's data then I create 1 Section Header taking today date
Below image data section header is Static or data also:
Above image data are getting using Below code :
childList.add(new Company("Ketul Inc.", "11/11/2017 3:46 PM")); childList.add(new Company("Atmel Corporation", "09/19/2017 8:46 PM")); childList.add(new Company("ABC Technologies", "09/12/2017 7:41 PM")); childList.add(new Company("Huron Capital Partners LLC", "09/12/2017 7:25 PM")); sectionHeaders = new ArrayList<>(); //Create a List of SectionHeader DataModel implements SectionHeader sectionHeaders.add(new CompanySectionHeader(childList, "SATURDAY 7 APRIL", 2));
Below Code is my
SectionHeader.Java
:public class CompanySectionHeader implements Section<Company>, Comparable<CompanySectionHeader> { List<Company> childList; String sectionText; int index; public CompanySectionHeader(List<Company> childList, String sectionText, int index) { this.childList = childList; this.sectionText = sectionText; this.index = index; } @Override public List<Company> getChildItems() { return childList; } public String getSectionText() { return sectionText; } @Override public int compareTo(CompanySectionHeader another) { if (this.index > another.index) { return -1; } else { return 1; } } }
Below is my SQLite Database Structure:
public String getFromatDate(long dateTime) { String formatedDate; Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(dateTime); Date mDate = calendar.getTime(); SimpleDateFormat sdf; sdf = new SimpleDateFormat("dd/MM/yyyy hh:mm a", new Locale("en")); formatedDate = sdf.format(mDate); return formatedDate; } public long insertCompany(Company company){ //String sql = null; SQLiteDatabase db = dbHelper.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(DatabaseHelper.KEY_COMPANY_NAME, company.getName()); values.put(DatabaseHelper.KEY_CREATED_AT, System.currentTimeMillis()); values.put(DatabaseHelper.KEY_UPDATED_AT, System.currentTimeMillis()); values.put(DatabaseHelper.KEY_COMPANY_WEBSITE,company.getWebsite()); values.put(DatabaseHelper.KEY_COMPANY_EMAIL,company.getEmail()); values.put(DatabaseHelper.KEY_COMPANY_PHONE_HOME,company.getPhoneHome()); long company_id = db.insert(COMPANY, null, values); return company_id; }
My Question is How to create section header dynamically
If u need any code or information you can ask me :)
Thanks in Advance :
解决方案I had Recently done this for sorting something on Monthly Basis. Its By overriding ViewType Function .
For doing this you have to use something like
getViewType()
in Recycler Adapter.public class LastTransAdapter extends RecyclerView.Adapter<LastTransAdapter.ViewHolder> { private ArrayList<LastTransBean> lastTransBeans; private Context context; private MyCustomTextView textViewTitle, tv_Desc, tv_Date, tv_Amount; private LinearLayout layout1; private View customView, myView; private LayoutParams layoutParams; private PopupWindow popUpWindow = null; private RelativeLayout layout; private int MONTHHEADER = 1; private int DATAVIEW = 2; public LastTransAdapter(ArrayList<LastTransBean> lastTransBeans, Context context, View myView, RelativeLayout layout) { this.lastTransBeans = lastTransBeans; this.context = context; this.myView = myView; this.layout = layout; } @Override public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { if (i == DATAVIEW) { // view for normal data. View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.single_row_last_transactions, viewGroup, false); return new ViewHolder(view); } else { // view type for month or date header View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.single_row_month_header, viewGroup, false); return new ViewHolder(view); } } @Override public void onBindViewHolder(ViewHolder viewHolder, int i) { final int position = i; if (viewHolder.getItemViewType() == DATAVIEW) { //fill data for normal view } else { //set your date or month header } } @Override public int getItemCount() { return lastTransBeans.size(); } @Override public int getItemViewType(int position) { if (lastTransBeans.get(position).getMonth()) { return MONTHHEADER; } else { return DATAVIEW; } } public class ViewHolder extends RecyclerView.ViewHolder { MyCustomTextView transtile, transdate, transamont, tv_monthHeader; LinearLayout acctype; CardView mlastTransCardView; public ViewHolder(View itemView) { super(itemView); // cast all the textviews , buttonsetc used ion view Holder. } } }
You can also use multiple viewholder types. Like this example.
Hope this may help you to solve your problem
这篇关于使用当前日期和时间的RecyclerView动态部分标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!