gpt4 book ai didi

android - android fragment 问题

转载 作者:行者123 更新时间:2023-11-29 00:23:25 26 4
gpt4 key购买 nike

我正在使用 android fragment 和查看寻呼机小部件。我想向定义的布局添加更多 fragment 。

The issue is

当我尝试通过代码输入 fragment 时,我总是遇到错误。

下面是我的代码

public class Video extends FragmentActivity {
MyPageAdapter pageAdapter;
ViewPager pager;
LinearLayout Video_content;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video);

Video_content = (LinearLayout)findViewById(R.id.Content);
FragmentTransaction t = getFragmentManager().beginTransaction();
//Add first fragment
MyFragment myFragment1 = new MyFragment();

我在这条线上遇到了错误

t.add(Video_content.getId(), myFragment1, "myFirstFragment");

/*Video_fragment myFragment2 = new Video_fragment();
t.add(Video_content.getId(), myFragment2, "mySecondFragment");*/
t.commit();

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int screenWidth = metrics.widthPixels;

Button button1=(Button)findViewById(R.id.Tvshows);
Button button2=(Button)findViewById(R.id.movies);
Button button3=(Button)findViewById(R.id.music);

List<Fragment> fragments = getFragments();

pageAdapter = new MyPageAdapter(getSupportFragmentManager(), fragments);

pager = (ViewPager)findViewById(R.id.viewpager);
pager.setAdapter(pageAdapter);
pager.setCurrentItem(0);
button1.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
pager.setCurrentItem(0);
}
});
button2.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
pager.setCurrentItem(1);
}
});
button3.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
pager.setCurrentItem(2);
}
});



}



private List<Fragment> getFragments(){
List<Fragment> fList = new ArrayList<Fragment>();

fList.add(MyFragment.newInstance("Tv Shows"));
fList.add(MyFragment.newInstance("Movies"));
fList.add(MyFragment.newInstance("Music Videos"));

return fList;
}

private class MyPageAdapter extends FragmentPagerAdapter {
private List<Fragment> fragments;

public MyPageAdapter(FragmentManager fm, List<Fragment> fragments) {
super(fm);
this.fragments = fragments;
}
@Override
public Fragment getItem(int position) {
return this.fragments.get(position);

}

@Override
public int getCount() {
return this.fragments.size();
}
}

错误提示:将 MyFragment1 的类型更改为 Fragment

我已经导入了以下导入

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;

这是 MyFragment 类的代码:

import android.support.v4.app.Fragment;

public class MyFragment extends Fragment{
public static final String EXTRA_MESSAGE = "EXTRA_MESSAGE";


View v;
Handler updateHandler;
public static final MyFragment newInstance(String message)
{
MyFragment f = new MyFragment();
Bundle bdl = new Bundle(1);
bdl.putString(EXTRA_MESSAGE, message);
f.setArguments(bdl);
return f;

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String message = getArguments().getString(EXTRA_MESSAGE);
v = inflater.inflate(R.layout.video_fragment, container, false);
TextView messageTextView = (TextView)v.findViewById(R.id.textView);
messageTextView.setText(message);

if(message.equals("Tv Shows"))
{

}
if(message.equals("Movies"))
{
}
if(message.equals("Music Videos"))
{

}

return v;
}

My Question is:

如何通过 fragment 添加通用布局并同时使用 View 分页器?

最佳答案

您确定您使用的是正确的 FragmentTransactionFragmentManager 类吗?

我相信

getFragmentManager().beginTransaction();

应该换成这个

getSupportFragmentManager().beginTransaction();

关于android - android fragment 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21524810/

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