你好,我刚刚开始编码,所以我称自己是初学者,我遇到了一些困难,我制作了一个ryclerview,其中包含一个图像和文本,并且有点滞后。我的图像是.jpeg 500x500。任何建议都会有所帮助。
适配器:
public class MenuAdapter extends RecyclerView.Adapter <MenuAdapter.MenuViewHolder>{
private ArrayList<MenuItem> mMenuList;
private MenuOnItemClickListener mListener;
private Context context ;
public interface MenuOnItemClickListener {
void MenuOnItemClick(int position);
}
public void MenuSetOnItemClickListener(MenuOnItemClickListener listener){
mListener = listener;
}
public static class MenuViewHolder extends RecyclerView.ViewHolder {
public ImageView mImageView;
public TextView mTextView1;
public TextView mTextView2;
public MenuViewHolder(View itemView, final MenuOnItemClickListener listener) {
super(itemView);
mImageView = itemView.findViewById(R.id.card_imageView2);
mTextView1 = itemView.findViewById(R.id.card_textView);
mTextView2 = itemView.findViewById(R.id.card_textView3);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (listener != null) {
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION){
listener.MenuOnItemClick(position);
}
}
}
});
}
}
public MenuAdapter(ArrayList<MenuItem> menuList){
mMenuList = menuList;
}
@Override
public MenuViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_menu, parent,false);
MenuViewHolder evh = new MenuViewHolder(v, mListener);
return evh;
}
@Override
public void onBindViewHolder(MenuViewHolder holder, int position) {
MenuItem cuurentItem = mMenuList.get(position);
holder.mTextView1.setText(cuurentItem.getText1());
holder.mTextView2.setText(cuurentItem.getText2());
holder.mImageView.setImageResource(cuurentItem.getImageResource());
}
@Override
public int getItemCount() {
return mMenuList.size();
}
主要活动: 滑翔机未在Reciclerview中使用
public class MenuActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private MenuAdapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
private BottomNavigationView.OnNavigationItemSelectedListener mItemSelectedListener;
private BottomNavigationView mBottomNavigationView;
private ImageView imagelogo;
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(LocaleHelper.onAttach(base, "en"));
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
//glide
imagelogo = (ImageView) findViewById(R.id.logo_top_imageView2);
Glide
.with(this)
.load(R.drawable.mikripoli_logo)
.into(imagelogo);
//main menu
final ArrayList<com.example.user.mikripoli.MenuItem> menuList = new ArrayList<>();
menuList.add(new com.example.user.mikripoli.MenuItem(R.drawable.iconksenodoxeio , R.string.Hotel, R.string.Allaboutourhotel ));
menuList.add(new com.example.user.mikripoli.MenuItem(R.drawable.iconaxiotheata , R.string.Sights , R.string.Theyworthwatching ));
menuList.add(new com.example.user.mikripoli.MenuItem(R.drawable.iconparalies , R.string.Rhodeshistory, R.string.Youshoulddo ));
menuList.add(new com.example.user.mikripoli.MenuItem(R.drawable.iconactivities , R.string.Activities , R.string.Youshoulddo ));
menuList.add(new com.example.user.mikripoli.MenuItem(R.drawable.iconmap , R.string.Maps, R.string.Menutext2 ));
menuList.add(new com.example.user.mikripoli.MenuItem(R.drawable.iconthlefona ,R.string.Usefultelephones,R.string.Youshoulddo ));
// main menu
mRecyclerView = findViewById(R.id.menu_recyclerView);
mRecyclerView.setNestedScrollingEnabled(false);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(this);
mAdapter = new MenuAdapter(menuList);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(mAdapter);
mAdapter.MenuSetOnItemClickListener(new MenuAdapter.MenuOnItemClickListener() {
// on click main menu
@Override
public void MenuOnItemClick (int position) {
menuList.get(position);
if (position == 0){
Intent hotelMenu = new Intent(mRecyclerView.getContext(), HotelMenu.class);
startActivityForResult(hotelMenu, 0);
}
if (position == 1){
Intent activities = new Intent(mRecyclerView.getContext(), SightsMenu.class);
startActivityForResult(activities, 1);
}
if (position == 2){
Intent activities = new Intent(mRecyclerView.getContext(), BeachMenu.class);
startActivityForResult(activities, 2);
}
if (position == 3){
Intent activities = new Intent(mRecyclerView.getContext(), ActivitiesMenu.class);
startActivityForResult(activities, 3);
}
if (position == 4) {
Intent activities = new Intent(mRecyclerView.getContext(), MapMain.class);
startActivityForResult(activities, 4);
}
if (position == 5) {
Intent activities = new Intent(mRecyclerView.getContext(), CallingMenu.class);
startActivityForResult(activities, 5);
}
}
});
//Bottom Navigation Bar
BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.BottomNavView_bar);
BottomNavigationViewHelper.disableShiftMode(bottomNavigationView);
Menu menu = bottomNavigationView.getMenu();
android.view.MenuItem menuItem = menu.getItem(0);
menuItem.setChecked(true);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull android.view.MenuItem item) {
switch (item.getItemId()) {
case R.id.ic_home:
finish();
case R.id.ic_back:
Intent intent2 = new Intent(MenuActivity.this, ChooseLanguage.class);
startActivity(intent2);
finish();
}
return false;
}
});
}
}