在线上有很多教程。但是,我尝试了很多,但它们都无法做到我的期望正确。
我是Android和App开发的新手,但是有一些开发网站和API的经验。我已经开发了一个网站,并想创建一个简单的Android应用程序,以显示为用户的活动提要。
因此,经过一番研究,似乎可以处理来自API的JSON数据列表的最佳方法是RecyClerview,并且CardViews将提高设计。
因此,我设置了一个带有底部磁带的项目,并创建了三个选项卡(事件,配置文件和通知(。这些标签返回片段,列表应在事件片段中。我已经对其进行了硬编码以显示5个项目,但它从未显示出卡片。因此,我不确定它在哪里错。其他片段正确显示
这是我的代码:
basefragment
public abstract class BaseFragment extends AppCompatActivity {
protected abstract Fragment createFragment();
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user);
FragmentManager fm = getSupportFragmentManager();
Fragment fragment = fm.findFragmentById(R.id.content);
if(fragment == null){
fragment = createFragment();
fm.beginTransaction()
.add(R.id.events_fragment_container, fragment)
.commit();
}
}
}
事件片段,适配器和View holder
public class EventsRecyclerFragment extends Fragment{
public static Fragment newInstance(){
return new EventsRecyclerFragment();
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.events_recycler_view_fragment, container, false);
RecyclerView recyclerView = view.findViewById(R.id.events_recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setAdapter(new EventsRecyclerViewAdapter());
return view;
}
private class EventsRecyclerViewHolder extends RecyclerView.ViewHolder{
private CardView mCardView;
private TextView mTextView;
public EventsRecyclerViewHolder(View itemView) {
super(itemView);
}
public EventsRecyclerViewHolder(LayoutInflater inflater, ViewGroup container){
super(inflater.inflate(R.layout.events_card_view, container, false));
mCardView = itemView.findViewById(R.id.events_card_container);
}
}
private class EventsRecyclerViewAdapter extends RecyclerView.Adapter<EventsRecyclerViewHolder>{
@Override
public EventsRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(getContext());
return new EventsRecyclerViewHolder(inflater, parent);
}
@Override
public void onBindViewHolder(EventsRecyclerViewHolder holder, int position) {
}
@Override
public int getItemCount() {
return 5;
}
}
}
我在活动上的使用
public class UserActivity extends AppCompatActivity {
public static final String PREFS_NAME = "Preferences";
private static final String PREF_EMAIL = "EMAIL";
private static final String PREF_PASSWORD = "PASSWORD";
private static final String PREF_TOKEN = "TOKEN";
Button btnLogout;
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
switch (item.getItemId()) {
case R.id.navigation_events:
transaction.replace(R.id.content, new EventsRecyclerFragment().newInstance()).commit();
return true;
case R.id.navigation_profile:
transaction.replace(R.id.content, new UserFragment()).commit();
return true;
case R.id.navigation_notifications:
transaction.replace(R.id.content, new NotificationsFragment()).commit();
return true;
}
return false;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user);
btnLogout = (Button) findViewById(R.id.btnLogin);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
}
}
谁能帮助我如何使这个基本列表显示?预先感谢
需要两个更改:
-
UserActivity
:在newInstance()
方法返回新实例本身时,从EventsRecyclerFragment()
实例初始化删除new
关键字。public class UserActivity extends AppCompatActivity { public static final String PREFS_NAME = "Preferences"; private static final String PREF_EMAIL = "EMAIL"; private static final String PREF_PASSWORD = "PASSWORD"; private static final String PREF_TOKEN = "TOKEN"; Button btnLogout; private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener = new BottomNavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(@NonNull MenuItem item) { FragmentManager manager = getSupportFragmentManager(); FragmentTransaction transaction = manager.beginTransaction(); switch (item.getItemId()) { case R.id.navigation_events: transaction.replace(R.id.content, EventsRecyclerFragment().newInstance()).commit(); return true; case R.id.navigation_profile: transaction.replace(R.id.content, new UserFragment()).commit(); return true; case R.id.navigation_notifications: transaction.replace(R.id.content, new NotificationsFragment()).commit(); return true; } return false; } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); BottomNavigationView navigation = findViewById(R.id.navigation); navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener); } }
-
events recyclerFragment:在其初始化期间始终设置回收室视图的
orientation
。public class EventsRecyclerFragment extends Fragment{ public static Fragment newInstance(){ return new EventsRecyclerFragment(); } @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.events_recycler_view_fragment, container, false); RecyclerView recyclerView = view.findViewById(R.id.events_recycler_view); LinearLayoutManager linearLayoutManager =new LinearLayoutManager(getActivity()); linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL); recyclerView.setLayoutManager(linearLayoutManager); recyclerView.setAdapter(new EventsRecyclerViewAdapter()); return view; } private class EventsRecyclerViewHolder extends RecyclerView.ViewHolder{ private CardView mCardView; private TextView mTextView; public EventsRecyclerViewHolder(View itemView) { super(itemView); } public EventsRecyclerViewHolder(LayoutInflater inflater, ViewGroup container){ super(inflater.inflate(R.layout.events_card_view, container, false)); mCardView = itemView.findViewById(R.id.events_card_container); } } private class EventsRecyclerViewAdapter extends RecyclerView.Adapter<EventsRecyclerViewHolder>{ @Override public EventsRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { LayoutInflater inflater = LayoutInflater.from(getContext()); return new EventsRecyclerViewHolder(inflater, parent); } @Override public void onBindViewHolder(EventsRecyclerViewHolder holder, int position) { } @Override public int getItemCount() { return 5; } } }