Android Open Source - AppWithTabsSample View Post Fragment






From Project

Back to project page AppWithTabsSample.

License

The source code is released under:

Apache License

If you think the Android project AppWithTabsSample listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package burhanloey.appwithtabssample;
//  ww  w.  j  ava  2 s.  c  o  m


import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;


/**
 * A simple {@link Fragment} subclass.
 *
 */
public class ViewPostFragment extends Fragment implements AdapterView.OnItemClickListener {

    private static final String loremIpsum = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";

    private ActionBarActivity activity;
    private ListView listView;
    private PostListAdapter postListAdapter;

    public ViewPostFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_view_post, container, false);

        activity = (ActionBarActivity) getActivity();
        setHasOptionsMenu(true);
        activity.supportInvalidateOptionsMenu();

        listView = (ListView) rootView.findViewById(R.id.listview_post);
        listView.setOnItemClickListener(this);
        postListAdapter = new PostListAdapter(getActivity(), R.layout.list_item_post);

        loadPost();

        return rootView;
    }

    private void loadPost() {
        for (int i = 0; i < 20; i++) {
            postListAdapter.add(loremIpsum);
        }
        listView.setAdapter(postListAdapter);
    }

    @Override
    public void onPrepareOptionsMenu(Menu menu) {
        activity.getSupportActionBar().setDisplayHomeAsUpEnabled(false);
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
        Fragment parentFragment = getParentFragment();
        FragmentManager manager = parentFragment.getChildFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right,
                android.R.anim.slide_in_left, android.R.anim.slide_out_right);
        transaction.replace(R.id.fragment_view_container, ViewCommentFragment.newInstance(position));
        transaction.addToBackStack(null);
        transaction.commit();
    }


    private class PostListAdapter extends ArrayAdapter<String> {

        private LayoutInflater inflater;

        public PostListAdapter(Context context, int resource) {
            super(context, resource);
            inflater = LayoutInflater.from(context);
        }

        @Override
        public int getItemViewType(int position) {
            return 0;
        }

        @Override
        public int getViewTypeCount() {
            return 1;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = inflater.inflate(R.layout.list_item_post, null);
                holder.titleTextView = (TextView) convertView.findViewById(R.id.post_title);
                holder.contentTextView = (TextView) convertView.findViewById(R.id.post_content);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.titleTextView.setText("#LuahanUM" + position);
            holder.contentTextView.setText(getItem(position));

            return convertView;
        }
    }

    private static class ViewHolder {
        public TextView titleTextView;
        public TextView contentTextView;
    }
}




Java Source Code List

burhanloey.appwithtabssample.ApplicationTest.java
burhanloey.appwithtabssample.HomeFragment.java
burhanloey.appwithtabssample.MainActivity.java
burhanloey.appwithtabssample.ViewCommentFragment.java
burhanloey.appwithtabssample.ViewFragment.java
burhanloey.appwithtabssample.ViewPostFragment.java
burhanloey.appwithtabssample.WriteFragment.java