Android Open Source - AppWithTabsSample View Comment 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;
//w w  w .  j  a  va  2 s .c  om


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.ActionBarActivity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


/**
 * A simple {@link Fragment} subclass.
 *
 */
public class ViewCommentFragment extends Fragment implements View.OnKeyListener {

    private ActionBarActivity activity;
    private TextView resultTextView;

    public static ViewCommentFragment newInstance(int position) {
        ViewCommentFragment fragment = new ViewCommentFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("position", position);
        fragment.setArguments(bundle);
        return fragment;
    }

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

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

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

        resultTextView = (TextView) rootView.findViewById(R.id.result_textview);
        resultTextView.setText("You have clicked #Luahan" + getArguments().getInt("position"));

        rootView.setFocusableInTouchMode(true);
        rootView.requestFocus();
        rootView.setOnKeyListener(this);

        return rootView;
    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        } else if (id == android.R.id.home) {
            backPress();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void backPress() {
        Fragment parent = getParentFragment();
        FragmentManager manager = parent.getChildFragmentManager();
        manager.popBackStack();
    }

    @Override
    public boolean onKey(View view, int keyCode, KeyEvent keyEvent) {
        if (keyCode == KeyEvent.KEYCODE_BACK && keyEvent.getAction() == KeyEvent.ACTION_UP) {
            backPress();

            return true;
        }

        return false;
    }
}




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