How to convert activities to fragments android

2019-08-06 17:20发布

问题:

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

}

}

then click on listview opens DetailActivity.class

回答1:

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" />


回答2:

I think FragmentManagers are what you are looking for =]

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

Note: Fragments don't really like talking to each other; you may need to implement an interface in the containing Activity to notify your ListAdapter that its dataset has changed



回答3:

Use FragmentActivity in place of Activity. And then create fragment by extending ListFragment. For list fragment use this link. And better to use this link

As its having same thing what you want. (Means one activity and ListFragment)