如何转换活动,以Android的碎片(How to convert activities to fr

2019-10-19 11:43发布

我想转换活动使用片段的另一片段加载RSS链接后应该打开ListActivity,但他必须在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;
    }

}

}

然后点击列表视图中打开DetailActivity.class

Answer 1:

这应该做的伎俩:

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;
}

}

然后加入这样的事情你的活动,以显示片段:

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


Answer 2:

我认为FragmentManagers是你在找什么=]

http://developer.android.com/reference/android/app/FragmentManager.html

注:碎片真的不喜欢彼此交谈; 您可能需要实现一个接口中包含活动通知您的ListAdapter其数据集已经改变



Answer 3:

在活动的地方使用FragmentActivity。 然后通过延长ListFragment创建片段。 对于名单片段使用这个链接 。 并更好地使用这个链接

由于其具有同样的事情,你想要什么。 (意指一个活性和ListFragment)



文章来源: How to convert activities to fragments android