【问题标题】:Converting Intent to Fragment in Android在 Android 中将 Intent 转换为 Fragment
【发布时间】:2013-11-15 15:05:13
【问题描述】:

一般来说,我大部分时间都使用 Intent 而不是 Fragment,因为我发现它有点复杂。我有一个带有 ListView 的类来显示数组中的一些数据,我想在 Fragment 中转换它,还想在新的碎片类中传递额外内容。

ListView 类:

public class MainActivity extends Activity {
    ListView list;
    String[] web = { "Google", "Twitter", "Windows", "Bing", "Itunes",
            "Wordpress", "Drupal" };
    Integer[] imageId = { R.drawable.ic_launcher, R.drawable.ic_launcher,
            R.drawable.ic_launcher, R.drawable.ic_launcher,
            R.drawable.ic_launcher, R.drawable.ic_launcher,
            R.drawable.ic_launcher

    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        CustomList adapter = new CustomList(MainActivity.this, web, imageId);
        list = (ListView) findViewById(R.id.list);
        list.setAdapter(adapter);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                Toast.makeText(MainActivity.this,
                        "You Clicked at " + web[+position], Toast.LENGTH_SHORT)
                        .show();


                Intent i = new Intent(MainActivity.this, intent.class);
                i.putExtra("name", web[+position]);
                startActivity(i);

            }
        });

    }

}

自定义列表类:

public class CustomList extends ArrayAdapter<String> {

    private final Activity context;
    private final String[] web;
    private final Integer[] imageId;

    public CustomList(Activity context, String[] web, Integer[] imageId) {
        super(context, R.layout.list_single, web);
        this.context = context;
        this.web = web;
        this.imageId = imageId;

    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();
        View rowView = inflater.inflate(R.layout.list_single, null, true);
        TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);

        ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
        txtTitle.setText(web[position]);

        imageView.setImageResource(imageId[position]);
        return rowView;
    }
}

我有一个主类,其中包含三个使用片段创建的选项卡,我想在这个类中添加 ListView!

public class LayoutOne extends Fragment {


    public static Fragment newInstance(Context context) {
        LayoutOne f = new LayoutOne();  

        return f;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        ViewGroup root = (ViewGroup) inflater.inflate(R.layout.layout_one, null);
        return root;

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

    }

}

我想做的是在片段类中显示列表视图,当单击列表项时,我想启动新意图以显示单击了哪个项!

【问题讨论】:

  • 我认为该片段只是一个在网络中找到的片段复制和粘贴虚拟
  • 这不是单例@Neil,这是创建片段并传递参数的官方设计模式。
  • 您每天都会学到新东西。我以前从未使用过。
  • 我使用片段是因为我想创建带有滑动视图的标签!在我的主要活动课上,我得到了三个标签,而这一个就是其中之一。我想在我的第一个选项卡上显示 ListView!

标签: android android-intent android-fragments android-fragmentactivity


【解决方案1】:

如果您想将参数传递到 Fragment,通常的做法是创建一个静态方法,该方法会生成具有某些参数的 Fragment 实例。您已经有了newInstance 方法,现在您只需向参数添加参数并返回Fragment。您应该检查onCreateView 中的参数。以下是您如何编写参数的示例。

public class LayoutOne extends Fragment 
{

    public static Fragment newInstance(int someInt, String someString) 
    {
        LayoutOne f = new LayoutOne();

        Bundle args = new Bundle();
        args.putInt("someInt", someInt);
        args.putString("someString", someString);
        f.setArguments(args);

        return f;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) 
    {
        ViewGroup root = (ViewGroup) inflater.inflate(R.layout.layout_one, null);
        Bundle args = getArguments();

        //From here you would check to see if your arguments are present 
        //proceed accordingly

        return root;

    }
}

对于您的具体情况,您可以传递字符串数组并将其添加为参数。

希望这会有所帮助。祝你好运!

编辑

您面临的问题是您在 Activity 中实例化了 ListView,而实际上您应该在 Fragment 中进行实例化。这里不需要意图,您只需要确保有一个ListView 属性可以从您附加到Fragment 的布局中获取。下面是一个小例子,说明它应该如何工作。

注意,这个方法在你的 LayoutOne 类中

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) 
{
    ViewGroup root = (ViewGroup) inflater.inflate(R.layout.layout_one, null);

    //Be sure to use the actual id of your list view
    ListView lv = (ListView) root.findViewById(R.id.list_view_id);

    //Make sure that you retrieve the values for 'web' and 'imageId' from 
    //the activity using the "newInstance" method.
    CustomList adapter = new CustomList(getActivity(), web, imageId);
    lv.setAdapter(adapter);

    //From here, set your onClickListener and stuff as you did in your Activity

    return root;

}

请注意,Fragments 没有 findViewById 方法。您必须在要膨胀的 View 上调用它以作为其布局,因此在这种情况下,您膨胀了 root,然后您将调用 root.findViewById(R.id.some_id) 以获取视图。

【讨论】:

  • 我遇到了错误!请重新检查我的代码以了解我的实际意思! @安德鲁·舒斯特
  • #1 - 构造函数 CustomList(LayoutOne, String[], Integer[]) 未定义 #2 - 方法 findViewById(int) 未定义类型 LayoutOne #3 - 方法 makeText(Context Toast 类型中的 , CharSequence, int) 不适用于参数 (LayoutOne, String, int)
  • 第一个错误是我无法帮你解决的问题。这是您在实施CustomList 时遇到的问题。我在上面的编辑中解决了第二个错误。第三个不起作用,因为Fragments 不扩展上下文。换成Toast.makeText(getActivity(), "your text", Toast.LENGTH_SHORT).show()
  • 非常感谢!您的代码已经解决了两个问题!现在我需要让 onClickListener 为 ListView 工作。我正在使用 ListView 类中的相同 onClickListener,它没有显示任何错误,但应用程序在启用 onClickListener 时崩溃!任何想法? @安德鲁·舒斯特
  • 您必须检查 LogCat 才能看到崩溃导致的错误。找到 LogCat,你就会知道你的错误。 @ProkashSarkar
猜你喜欢
  • 2021-07-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-25
  • 2023-04-03
  • 1970-01-01
  • 2020-02-22
  • 1970-01-01
相关资源
最近更新 更多