网上有很多关于此内容的教程。但是,我尝试了很多,但没有一个能做到我所期望的。
我对Android和App开发很陌生,但在开发网站和API方面有一些经验。我开发了一个网站,想要创建一个简单的Android应用程序来显示用户的事件。
经过一些研究,似乎处理来自API的高效JSON数据的最佳方法是使用RecyclerView,而CardViews会增加一些设计。
因此,我设置了一个带有底部导航栏的项目,并创建了三个选项卡(事件,配置文件和通知)。这些选项卡返回Fragment,List应位于Events Fragment中。我已经硬编码显示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();
}
}
}
事件片段,适配器和ViewHolder
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);
}
}
有谁可以帮助我如何让这个基本列表出现? 提前致谢
答案 0 :(得分:1)
需要进行两项更改:
UserActivity
:从new
实例初始化中删除EventsRecyclerFragment()
关键字,因为newInstance()
方法本身会返回一个新实例。
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);
}
}
EventsRecyclerFragment:在初始化过程中始终设置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;
}
}
}