我一直在开发一个使用了回收站视图的应用。这就是我希望看到的内容:
但是现在,我的代码显示如下:
我喜欢卡在此处,在recyclerview中没有正确创建视图{在启动时像3-4个空白标签一样),我需要我的应用程序看起来更好。我在下面附上我的代码: MainActivity代码:
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private CustomAdapter customAdapter;
ListView listView;
public Cursor cursor;
public StudentRepo studentRepo;
private final static String TAG = MainActivity.class.getName().toString();
private static final String TAG_BOOKMARKS="bookmarks";
private static final String TAG_ABOUT_US="about";
//recyclerView implementation
private List<TopSample> wordlist=new ArrayList<>();
private RecyclerView recyclerView;
private StudentAdapter studentAdapter;
//recyclerView implementation done
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
studentRepo = new StudentRepo(this);
recyclerView=(RecyclerView)findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
DatabaseAccess databaseAccess = DatabaseAccess.getInstance(this);
databaseAccess.open();
Cursor cursor= databaseAccess.getInfo();
cursor.moveToFirst();
do{
TopSample topSample=new TopSample(cursor.getString(0));
wordlist.add(topSample);
}while (cursor.moveToNext());
databaseAccess.close();
studentAdapter=new StudentAdapter(wordlist);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(studentAdapter);
recyclerView.addOnItemTouchListener(new RecyclerTouchListener(getApplicationContext(), recyclerView, new ClickListener() {
@Override
public void onClick(View view, int position) {
TopSample topSample= wordlist.get(position);
Intent intent = new Intent(MainActivity.this, SearchResult.class);
intent.putExtra(SearchResult.EXTRA_NO, (int) position);
startActivity(intent);
}
@Override
public void onLongClick(View view, int position) {
}
}));
//Done recyclerview implement
ImageView imageView=new ImageView(this);
imageView.setImageResource(R.drawable.button_action);
FloatingActionButton actionButton = new FloatingActionButton.Builder(this)
.setContentView(imageView)
.build();
ImageView Bookmarks=new ImageView(this);
Bookmarks.setImageResource(R.drawable.button_action);
ImageView Aboutus=new ImageView(this);
Aboutus.setImageResource(R.drawable.button_action);
SubActionButton.Builder itemBuilder = new SubActionButton.Builder(this);
SubActionButton buttonbookmark = itemBuilder.setContentView(Bookmarks).build();
SubActionButton buttonaboutus = itemBuilder.setContentView(Aboutus).build();
buttonbookmark.setOnClickListener(this);
buttonaboutus.setOnClickListener(this);
buttonbookmark.setTag(TAG_BOOKMARKS);
buttonaboutus.setTag(TAG_ABOUT_US);
FloatingActionMenu actionMenu = new FloatingActionMenu.Builder(this)
.addSubActionView(buttonbookmark)
.addSubActionView(buttonaboutus)
.attachTo(actionButton)
.build();
}
public interface ClickListener{
void onClick(View view, int position);
void onLongClick (View view, int position);
}
public static class RecyclerTouchListener implements RecyclerView.OnItemTouchListener{
private GestureDetector gestureDetector;
private MainActivity.ClickListener clickListener;
public RecyclerTouchListener(Context context, final RecyclerView recyclerView,final MainActivity.ClickListener clickListener){
this.clickListener = clickListener;
gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onSingleTapUp(MotionEvent e) {
return true;
}
@Override
public void onLongPress(MotionEvent e) {
View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
if (child != null && clickListener != null) {
clickListener.onLongClick(child, recyclerView.getChildPosition(child));
}
}
});
}
@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
View child = rv.findChildViewUnder(e.getX(), e.getY());
if (child != null && clickListener != null && gestureDetector.onTouchEvent(e)) {
clickListener.onClick(child, rv.getChildPosition(child));
}
return false;
}
@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
}
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
}
@Override
public void onResume() {
super.onResume();
}
@Override
///@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.options_menu, menu);
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
SearchManager manager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView search = (SearchView) menu.findItem(R.id.search).getActionView();
search.setSearchableInfo(manager.getSearchableInfo(getComponentName()));
search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
Log.d(TAG, "onQueryTextSubmit ");
//cursor = studentRepo.getStudentListByKeyword(s);
// if (cursor == null) {
// Toast.makeText(MainActivity.this, "No records found!", Toast.LENGTH_LONG).show();
// } else {
// Toast.makeText(MainActivity.this, cursor.getCount() + " records found!", Toast.LENGTH_LONG).show();
// }/
//customAdapter.swapCursor(cursor);
return false;
}
@Override
public boolean onQueryTextChange(String s) {
Log.d(TAG, "onQueryTextChange ");
final List<TopSample>filteredmodellist=filter(wordlist,s);
studentAdapter.setFilter(filteredmodellist);
// cursor = studentRepo.getStudentListByKeyword(s);
// if (cursor != null) {
// customAdapter.swapCursor(cursor);
// }
return true;
}
private List<TopSample>filter(List<TopSample>models,String query){
query=query.toLowerCase();
final List<TopSample>fliterdModelList=new ArrayList<>();
for(TopSample model:models){
final String text=model.getVocab().toLowerCase();
if (text.contains(query)){
fliterdModelList.add(model);
}
}
return fliterdModelList;
}
});
return true;
}
@Override
public void onClick(View v) {
if (v.getTag().equals(TAG_BOOKMARKS)){
startActivity(new Intent(this,Bookmarks.class));
}
if (v.getTag().equals(TAG_ABOUT_US)){
}
}
}
StudentAdapter文件:
public class StudentAdapter extends RecyclerView.Adapter<StudentAdapter.MyViewHolder> {
private List<TopSample> wordslist= Collections.emptyList();
public class MyViewHolder extends RecyclerView.ViewHolder{
public TextView sword;
public MyViewHolder(View view){
super(view);
sword=(TextView)view.findViewById(R.id.vocab);
}
}
public StudentAdapter(List<TopSample> wordslist){
this.wordslist=wordslist;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView=LayoutInflater.from(parent.getContext()).inflate(R.layout.dictionary_list_row,parent,false);
MyViewHolder vh=new MyViewHolder(itemView);
return vh;
//return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
TopSample sample=wordslist.get(position);
holder.sword.setText(sample.getVocab());
}
@Override
public int getItemCount() {
return wordslist.size();
}
//SearchinRecyclerview
public void setFilter(List<TopSample> mTopSamples){
wordslist=new ArrayList<>();
wordslist.addAll(mTopSamples);
notifyDataSetChanged();
}
//SearchinRecyclerview end
}
dictionary_list_row.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:clickable="true"
android:focusableInTouchMode="true"
android:background="?android:attr/selectableItemBackground"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/vocab"
android:textStyle="bold"
android:textSize="20sp"
/>
</LinearLayout>
content_main.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_main"
tools:context=".MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
答案 0 :(得分:0)
更新dictionary_list_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical">
<TextView
android:id="@+id/vocab"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Country Name"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Code"
android:textSize="20sp"
android:textStyle="bold" />
</LinearLayout>
更新:
在此处进行一些更改添加if
部分
DatabaseAccess databaseAccess = DatabaseAccess.getInstance(this);
databaseAccess.open();
Cursor cursor= databaseAccess.getInfo();
cursor.moveToFirst();
do{
if(cursor.getString(0) != null){
TopSample topSample=new TopSample(cursor.getString(0));
wordlist.add(topSample);
}
}while (cursor.moveToNext());
databaseAccess.close();