我是android的初学者,正在测试RecyclerView.Following是我的片段:
<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"
tools:context="com.example.recyclerviewtest.RecyclerTestActivity" >
<android.support.v7.widget.RecyclerView
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:id="@+id/drawer"
>
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
以下是我的MainActivity.java类
package com.example.recyclerviewtest;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.os.StrictMode;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(savedInstanceState == null)
{
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.container, new RecyclerTestActivity()).commit();
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
}
}
}
这是main_activity_xml:
<LinearLayout 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"
tools:context="${relativePackage}.${activityClass}"
android:orientation="horizontal">
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/container"
/>
</LinearLayout>
但是在片段中的主要活动中附加片段时:
transaction.replace(R.id.container, new RecyclerTestActivity()).commit();
我收到错误:
The method replace is not applicable for (int,RecyclerTestActivity)
有人可以帮助我。
编辑:RecyclerTestActivity Fragment的代码是:
package com.example.recyclerviewtest;
import java.util.ArrayList;
import java.util.List;
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;
public class RecyclerTestActivity extends Fragment {
private Adapter adapter;
private RecyclerView recyclerview;
@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View Layout = inflater.inflate(R.layout.activity_recycler_test,container,false);
recyclerview=(RecyclerView)Layout.findViewById(R.id.drawer);
adapter=new Adapter(getActivity(), getdata());
recyclerview.setAdapter(adapter);
recyclerview.setLayoutManager(new LinearLayoutManager(getActivity()));
return Layout;
}
public List<Information> getdata()
{
List<Information> items = new ArrayList<>();
int[] data ={R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher};
String[] content={"One","Two","Three"};
for(int i=0;i<data.length;i++)
{
Information current = new Information();
current.desc=content[i];
current.number=data[i];
items.add(current);
}
return items;
}
}
答案 0 :(得分:2)
你的问题在于这一行:
transaction.replace(R.id.container, new RecyclerTestActivity()).commit();
这应该是transaction.replace(R.id.container, fragment).commit();
而不是Fragment
,而是Activity
。
同样在您的xml中,您使用的是<fragment>
标记。而是使用某些布局,例如<FrameLayout>
或<LinearLayout>
更新:
在MainActivity
中使用android.app.FragmentTransaction;
。但你的片段是android.support.v4.app.Fragment;
。你不能这样做。您必须完全使用AppCompat_v4。请改用android.app.Fragment
。
或者我强烈建议您MainActivity
延长FragmentActivity
并使用getSupportFragmentManager()
代替getFragmentManager()
。
答案 1 :(得分:1)
在 main_activity_xml :
中使用此功能<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fragmentcontainer">
</FrameLayout>
比 MainActivity.java 更换当前片段(用YourFragment
更改RecyclerTestActivity
):
Fragment fr;
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fr = new YourFragment();
fragmentTransaction.replace(R.id.fragmentcontainer, fr);
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
fragmentTransaction.commit();
记得导入:
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
YourFragment.java
是一个扩展Fragment
(android.app.Fragment)的类,请点击Fragment Docs
Fragment类示例(Google Docs示例):
public static class ExampleFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.example_fragment, container, false);
}
}
所以你的代码是:
<强> MainActivity.java 强>
package com.example.recyclerviewtest;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.os.StrictMode;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(savedInstanceState == null)
{
Fragment fr;
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fr = new RecyclerTestActivity();
fragmentTransaction.replace(R.id.fragmentcontainer, fr);
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
fragmentTransaction.commit();
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
}
}
}
<强> main_activity_layout.xml 强>:
<LinearLayout 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"
tools:context="${relativePackage}.${activityClass}"
android:orientation="horizontal">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fragmentcontainer">
</FrameLayout>
</LinearLayout>
您的 RecyclerTestActivity.java :
package com.example.recyclerviewtest;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class RecyclerTestActivity extends Fragment {
private Adapter adapter;
private RecyclerView recyclerview;
@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View Layout = inflater.inflate(R.layout.activity_recycler_test,container,false);
recyclerview=(RecyclerView)Layout.findViewById(R.id.drawer);
adapter=new Adapter(getActivity(), getdata());
recyclerview.setAdapter(adapter);
recyclerview.setLayoutManager(new LinearLayoutManager(getActivity()));
return Layout;
}
public List<Information> getdata()
{
List<Information> items = new ArrayList<>();
int[] data ={R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher};
String[] content={"One","Two","Three"};
for(int i=0;i<data.length;i++)
{
Information current = new Information();
current.desc=content[i];
current.number=data[i];
items.add(current);
}
return items;
}
}
答案 2 :(得分:0)
一个Activity不是片段因此你不能将它用作片段..你需要创建一个片段来将它添加到你的FragmentManager中。请参阅here。