私はこの時点でちょっと痛いです。時間を節約するために、私はAndroid Studioのテンプレートをナビゲーション引き出しアクティビティ用に使用しました。私はナビゲーション・ドロワを作成し、それを他のアクティビティに渡す予定でした。最初の活動はうまくいった。問題は、ナビゲーション・ドロワーを持ってRecyclerViewを作成したいので、2番目の問題です。 だから私のコードは次のとおりです。RecyclerViewは何も表示していませんが、問題はないようです。
Content_main2.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.sparrowred.sendcard.Main2Activity"
tools:showIn="@layout/activity_main2">
<android.support.v7.widget.RecyclerView
android:id="@+id/imageRecycleView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
app_bar_main2.xml:
nav_header_main2.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="@dimen/nav_header_height"
android:background="@drawable/side_nav_bar"
android:gravity="bottom"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:theme="@style/ThemeOverlay.AppCompat.Dark">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="@dimen/nav_header_vertical_spacing"
android:src="@android:drawable/sym_def_app_icon" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/nav_header_vertical_spacing"
android:text="Android Studio"
android:textAppearance="@style/TextAppearance.AppCompat.Body1" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="[email protected]" />
</LinearLayout>
activity_main2.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="@layout/app_bar_main2"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
image_list_row:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:clickable="true"
android:background="?android:attr/selectableItemBackground">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:contentDescription="@string/image_descrprtion"
android:layout_gravity="center_horizontal"
android:id="@+id/imageId"
android:src="@drawable/profile"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/title"
android:textStyle="bold"
android:padding="8dp"
android:layout_gravity="center_horizontal"
android:text="dummy"/>
</LinearLayout>
imageAdapter.java:
package com.sparrowred.sendcard;
public class imageAdapter {
private String title;
private int imageId;
public imageAdapter() {
}
public imageAdapter(String title, int imageId) {
this.title = title;
this.imageId = imageId;
}
public void setTitle(String title){
this.title = title;
}
public String getTitle(){
return this.title;
}
public void setImageId(int imageId){
this.imageId = imageId;
}
public int getImageId(){
return this.imageId;
}
}
imageDescriptionArray.java(私は名前を変更する必要があります:Pを):
package com.sparrowred.sendcard;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
public class imageDescriptionArray extends RecyclerView.Adapter<imageDescriptionArray.MyViewHolder> {
private LayoutInflater layoutInflater;
private List<imageAdapter> imageAdapterList;
public imageDescriptionArray(List<imageAdapter> imageAdapterList) {
this.imageAdapterList = imageAdapterList;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.image_list_row, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
imageAdapter iva = imageAdapterList.get(position);
holder.title.setText(iva.getTitle());
holder.image.setImageResource(iva.getImageId());
}
@Override
public int getItemCount() {
return imageAdapterList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView title;
public ImageView image;
public MyViewHolder(View view) {
super(view);
title = (TextView) view.findViewById(R.id.title);
image = (ImageView) view.findViewById(R.id.imageId);
}
}
}
、最終的にMain2Activity.java :
package com.sparrowred.sendcard;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import java.util.ArrayList;
import java.util.List;
import jp.wasabeef.recyclerview.animators.SlideInUpAnimator;
public class Main2Activity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private RecyclerView mImageRecycleView;
private List<imageAdapter> imageAdapterList;
private imageDescriptionArray mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
mImageRecycleView = (RecyclerView) findViewById(R.id.imageRecycleView);
mImageRecycleView.setHasFixedSize(true);
imageAdapterList = new ArrayList<>(12);
mAdapter = new imageDescriptionArray(imageAdapterList);
LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);
mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mImageRecycleView.setLayoutManager(mLayoutManager);
mImageRecycleView.setItemAnimator(new SlideInUpAnimator());
mImageRecycleView.setAdapter(mAdapter);
prepareData();
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main2, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camera) {
// Handle the camera action
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
private void prepareData(){
List<imageAdapter> data = new ArrayList<>();
int[] images = {R.drawable.birthday_image_1,R.drawable.birthday_image_2,R.drawable.birthday_image_3,R.drawable.birthday_image_4,
R.drawable.birthday_image_5,R.drawable.birthday_image_6,R.drawable.birthday_i mage_7,R.drawable.birthday_image_8,
R.drawable.birthday_image_9,R.drawable.birthday_image_10,R.drawable.birthday_ image_11,R.drawable.birthday_image_12};
String[] description = {"ballons","a lot of cakes","ballons with cakes","presents","presents with hat","cup-cake",
"retro","perspective","flying presents","empty card","giving a present","tasty cake"};
for(int i = 0; i<images.length && i<description.length; i++){
imageAdapter current = new imageAdapter();
current.setTitle(description[i]);
current.setImageId(images[i]);
data.add(current);
}
mAdapter.notifyDataSetChanged();
}
}
これは私の2番目のアクティビティのコードです。これは完全には実装されていませんが、必ずrecyclerViewは機能しません。誰もがこれで私を助けることができますか?問題は自分のxmls内にあると信じています....しかし、私は何も見つけることができません。 PS。私のコードはコンパイル時にエラーを表示しません....
私は動作しません。さらに、私は "I/Choreographer:46フレームをスキップしました!アプリケーションはメインスレッドであまりにも多くの作業をしているかもしれません"というメッセージを受け取りました... 私が気づいた唯一の事は、右上に小さな灰色の長方形のようなものがあり、1秒後に消えます。 –
次に、XMLに問題があるかもしれません。 –
onBindViewHolderにブレークポイントを設定して、呼び出しを取得していることを確認してください。 –