gpt4 book ai didi

java - 带工具栏的 RecyclerView

转载 作者:太空宇宙 更新时间:2023-11-04 13:32:25 25 4
gpt4 key购买 nike

我是 Material 设计新手,每次我尝试使用 RecyclerView 时,一切都会出错..

谁能知道问题出在哪里吗?

主要 Activity 类

 import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends ActionBarActivity {

Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar= (Toolbar) findViewById(R.id.toool);
setSupportActionBar(toolbar);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, 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);
}
}

主要 Activity 布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
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"
tools:context=".MainActivity" >

<include
android:id="@+id/toool"
layout="@layout/app_tool" />

<fragment
android:id="@+id/fragment"
android:name="com.example.hothyfa.recyclerandmore.Recycler"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/toool"
android:layout_centerHorizontal="true"
tools:layout="@layout/rere" />

</RelativeLayout>

回收器 fragment

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<android.support.v7.widget.RecyclerView
android:id="@+id/recye"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</android.support.v7.widget.RecyclerView>

</RelativeLayout>

回收类

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

public class Recycler extends Fragment {

private RecyclerView recyclerView;

private MyRecyeAdapter adapter;
private View view;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

view = inflater.inflate(R.layout.rere, container, false);

recyclerView = (RecyclerView) getActivity().findViewById(R.id.recye);

adapter = new MyRecyeAdapter(getActivity(), getData());
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

return view;
}

public static List<info> getData() {

List<info> data = new ArrayList<>();
int photos[] = { R.drawable.images, R.drawable.dreamsmal, R.drawable.images, R.drawable.dreamsmal };
String users[] = { "Hothyfa", "Marwan", "Jamal", "Alaa" };

for (int i = 0; i < photos.length && i < users.length; i++) {

info current = new info();
current.iconId = photos[i];
current.Names = users[i];
data.add(current);

}
return data;

}

}

RecyclerView适配器类

import android.content.Context;

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.Collections;

import java.util.List;

public class MyRecyeAdapter extends RecyclerView.Adapter<MyRecyeAdapter.myholder> {

List<info> data = Collections.emptyList();
private LayoutInflater inflater;

public MyRecyeAdapter(Context context, List<info> data) {

inflater = LayoutInflater.from(context);
this.data = data;

}

@Override
public myholder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.custom, parent, false);
myholder holder = new myholder(view);

return holder;
}

@Override
public void onBindViewHolder(myholder holder, int position) {

info current = data.get(position);

holder.imageProfile.setImageResource(current.iconId);

holder.UsersName.setText(current.Names);
}

@Override
public int getItemCount() {
return data.size();
}

public class myholder extends RecyclerView.ViewHolder {

ImageView imageProfile;
TextView UsersName;

public myholder(View itemView) {

super(itemView);
imageProfile = (ImageView) itemView.findViewById(R.id.imageView);
UsersName = (TextView) itemView.findViewById(R.id.textView);

}
}

}

自定义行布局(针对 RecyclerView)

 <?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="match_parent"
android:orientation="horizontal" >

<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:padding="8dp"
android:src="@drawable/images" />

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:padding="8dp"
android:text="Large Text" />

</LinearLayout>

回收器的数据类

public class info {

int iconId;

String Names;

}

工具栏布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/primarycolor" >
</android.support.v7.widget.Toolbar>

注意:我已将其放入 build.gradle

compile 'com.android.support:recyclerview-v7:22.2.1'

并重建应用程序。

错误:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hothyfa.recyclerandmore/com.example.hothyfa.recyclerandmore.MainActivity}: android.view.InflateException: Binary XML file line #19: Error inflating class fragment



at


app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)

at
\\\\\\\android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: android.view.InflateException: Binary XML file line #19: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:249)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106)
at com.example.hothyfa.recyclerandmore.MainActivity.onCreate(MainActivity.java:19)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
    

最佳答案

好的,我在您的 RecycleronCreateView 中找到了问题

recyclerView= (RecyclerView) getActivity().findViewById(R.id.recye);

将其更改为:

recyclerView= (RecyclerView) view.findViewById(R.id.recye);

关于java - 带工具栏的 RecyclerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32044017/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com