如何将活动转换为片段android [英] How to convert activities to fragments android

查看:120
本文介绍了如何将活动转换为片段android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想转换活动使用片段
加载RSS链接之后的另一个片段应该打开ListActivity,但他必须在fargment

I would like to convert activity to use Fragments another fragment after loading the RSS link should open the ListActivity but he must be in fargment

public class ListActivity extends Activity {

Application myApp;
RSSFeed feed;
ListView lv;
CustomListAdapter adapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.feed_list);

    myApp = getApplication();

    // Get feed form the file
    feed = (RSSFeed) getIntent().getExtras().get("feed");

    // Initialize the variables:
    lv = (ListView) findViewById(R.id.listView);
    lv.setVerticalFadingEdgeEnabled(true);

    // Set an Adapter to the ListView
    adapter = new CustomListAdapter(this);
    lv.setAdapter(adapter);

    // Set on item click listener to the ListView
    lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // actions to be performed when a list item clicked
            int pos = arg2;

            Bundle bundle = new Bundle();
            bundle.putSerializable("feed", feed);
            Intent intent = new Intent(ListActivity.this,
                    DetailActivity.class);
            intent.putExtras(bundle);
            intent.putExtra("pos", pos);
            startActivity(intent);

        }
    });

}

@Override
protected void onDestroy() {
    super.onDestroy();
    adapter.imageLoader.clearCache();
    adapter.notifyDataSetChanged();
}

class CustomListAdapter extends BaseAdapter {

    private LayoutInflater layoutInflater;
    public ImageLoader imageLoader;

    public CustomListAdapter(ListActivity activity) {

        layoutInflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imageLoader = new ImageLoader(activity.getApplicationContext());
    }

    @Override
    public int getCount() {

        // Set the total list item count
        return feed.getItemCount();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        // Inflate the item layout and set the views
        View listItem = convertView;
        int pos = position;
        if (listItem == null) {
            listItem = layoutInflater.inflate(R.layout.list_item, null);
        }

        // Initialize the views in the layout
        ImageView iv = (ImageView) listItem.findViewById(R.id.thumb);
        // set the ImageView opacity to 50%
        TextView tvTitle = (TextView) listItem.findViewById(R.id.title);
        TextView tvDate = (TextView) listItem.findViewById(R.id.date);

        // Set the views in the layout
        imageLoader.DisplayImage(feed.getItem(pos).getImage(), iv);
        tvTitle.setText(feed.getItem(pos).getTitle());
        tvDate.setText(feed.getItem(pos).getDate());

        return listItem;
    }

}

}

然后点击listview打开DetailActivity.class

then click on listview opens DetailActivity.class

推荐答案

这应该是诀窍: p>

This should do the trick:

public class ListFragment extends Fragment {

Application myApp;
RSSFeed feed;
ListView lv;
CustomListAdapter adapter;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.feed_list, container, false);

    myApp = getApplication();

    // Get feed form the file
    feed = (RSSFeed) getIntent().getExtras().get("feed");

    // Initialize the variables:
    lv = (ListView) findViewById(R.id.listView);
    lv.setVerticalFadingEdgeEnabled(true);

    // Set an Adapter to the ListView
    adapter = new CustomListAdapter(this);
    lv.setAdapter(adapter);

    // Set on item click listener to the ListView
    lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

            // actions to be performed when a list item clicked
            int pos = arg2;

            Bundle bundle = new Bundle();
            bundle.putSerializable("feed", feed);
            Intent intent = new Intent(ListActivity.this, DetailActivity.class);
            intent.putExtras(bundle);
            intent.putExtra("pos", pos);
            startActivity(intent);

        }
    });

}

@Override
protected void onDestroy() {
    super.onDestroy();
    adapter.imageLoader.clearCache();
    adapter.notifyDataSetChanged();
}

class CustomListAdapter extends BaseAdapter {

private LayoutInflater layoutInflater;
public ImageLoader imageLoader;

public CustomListAdapter(ListFragment fragment) {

    layoutInflater = (LayoutInflater) fragment
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    imageLoader = new ImageLoader(activity.getApplicationContext());
}

@Override
public int getCount() {

    // Set the total list item count
    return feed.getItemCount();
}

@Override
public Object getItem(int position) {
    return position;
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    // Inflate the item layout and set the views
    View listItem = convertView;
    int pos = position;
    if (listItem == null) {
        listItem = layoutInflater.inflate(R.layout.list_item, null);
    }

    // Initialize the views in the layout
    ImageView iv = (ImageView) listItem.findViewById(R.id.thumb);
    // set the ImageView opacity to 50%
    TextView tvTitle = (TextView) listItem.findViewById(R.id.title);
    TextView tvDate = (TextView) listItem.findViewById(R.id.date);

    // Set the views in the layout
    imageLoader.DisplayImage(feed.getItem(pos).getImage(), iv);
    tvTitle.setText(feed.getItem(pos).getTitle());
    tvDate.setText(feed.getItem(pos).getDate());

    return listItem;
}

}

然后将这样的东西添加到您的活动显示片段:

Then add something like this to your activity to show the fragment:

<fragment android:name="com.example.ListFragment"
        android:id="@+id/list"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="match_parent" />

这篇关于如何将活动转换为片段android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆