问题描述:

The Main tab Activity

 import android.content.Intent;

import android.support.design.widget.TabLayout;

import android.support.v7.app.AppCompatActivity;

import android.support.v4.app.Fragment;

import android.support.v4.app.FragmentManager;

import android.support.v4.app.FragmentPagerAdapter;

import android.support.v4.view.ViewPager;

import android.os.Bundle;

import android.util.Log;

import android.view.LayoutInflater;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.view.ViewGroup;

public class UserAreaTab extends AppCompatActivity {

Intent feed = new Intent(this,feedFrag.class);

private static final String TAG ="mytag";

private SectionsPagerAdapter mSectionsPagerAdapter;

/**

* The {@link ViewPager} that will host the section contents.

*/

private ViewPager mViewPager;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_user_area_tab);

mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

// Set up the ViewPager with the sections adapter.

mViewPager = (ViewPager) findViewById(R.id.container);

mViewPager.setAdapter(mSectionsPagerAdapter);

TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);

tabLayout.setupWithViewPager(mViewPager);

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.menu_user_area_tab, menu);

return true;

}

@Override

public boolean onOptionsItemSelected(MenuItem item) {

int id = item.getItemId();

//noinspection SimplifiableIfStatement

if (id == R.id.action_settings) {

return true;

}

return super.onOptionsItemSelected(item);

}

public class SectionsPagerAdapter extends FragmentPagerAdapter {

public SectionsPagerAdapter(FragmentManager fm) {

super(fm);

}

@Override

public Fragment getItem(int position) {

switch (position) {

case 2: feedFrag ff =new feedFrag();

return ff;

default: return PlaceholderFragment.newInstance(position + 1);

}

}

@Override

public int getCount() {

// Show 3 total pages.

return 3;

}

@Override

public CharSequence getPageTitle(int position) {

switch (position) {

case 0:

return "Message";

case 1:

return "Feed";

case 2:

return "Friends";

}

return null;

}

}

}

Then I have a place holder Fragment class

 import android.os.Bundle;

import android.support.v4.app.Fragment;

import android.util.Log;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

public class PlaceholderFragment extends Fragment {

private static final String ARG_SECTION_NUMBER = "section_number";

public PlaceholderFragment() {

}

public static PlaceholderFragment newInstance(int sectionNumber) {

PlaceholderFragment fragment = new PlaceholderFragment();

Bundle args = new Bundle();

args.putInt(ARG_SECTION_NUMBER, sectionNumber);

fragment.setArguments(args);

return fragment;

}

@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container,

Bundle savedInstanceState) {

if (getArguments().getInt(ARG_SECTION_NUMBER) == 1) {

View rootView = inflater.inflate(R.layout.fragment_message, container, false);

return rootView;

} else if (getArguments().getInt(ARG_SECTION_NUMBER) == 2) {

View rootView = inflater.inflate(R.layout.fragment_feed, container, false);

return rootView;

} else {

View rootView = inflater.inflate(R.layout.fragment_friends, container, false);

return rootView;

}

}

}

Then lastly the feedFrag class that is not being called.

 import android.content.Context;

import android.net.Uri;

import android.os.Bundle;

import android.support.v4.app.ListFragment;

import android.util.Log;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.BaseAdapter;

import android.widget.ListView;

import android.widget.TextView;

import java.util.LinkedList;

import java.util.List;

public class feedFrag extends ListFragment {

private static final String ARG_PARAM1 = "param1";

private static final String ARG_PARAM2 = "param2";

private static final String TAG ="mytag";

//

ListView lv;

List<String> userN = new LinkedList<String>();

String [] status={"test1","test2"};

private View view;

//

// TODO: Rename and change types of parameters

private String mParam1;

private String mParam2;

private OnFragmentInteractionListener mListener;

public feedFrag() {

// Required empty public constructor

}

// TODO: Rename and change types and number of parameters

public static feedFrag newInstance(String param1, String param2) {

feedFrag fragment = new feedFrag();

Bundle args = new Bundle();

args.putString(ARG_PARAM1, param1);

args.putString(ARG_PARAM2, param2);

fragment.setArguments(args);

return fragment;

}

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

if (getArguments() != null) {

mParam1 = getArguments().getString(ARG_PARAM1);

mParam2 = getArguments().getString(ARG_PARAM2);

Log.i(TAG,"Made it to on startmy");

}

}

@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container,

Bundle savedInstanceState) {

// Inflate the layout for this fragment

view =inflater.inflate(R.layout.fragment_feed,container,false);

Log.i(TAG,"Made it to createdview");

TextView Wel =(TextView)view.findViewById(R.id.WelcomeTxt);

Wel.setText("Code is good");

return view;

}

@Override

public void onActivityCreated( Bundle savedInstanceState) {

super.onActivityCreated(savedInstanceState);

userN.add("Hello :)");

userN.add("Choppy");

String []

username = userN.toArray(new String[userN.size()]);

Log.i(TAG,"Made it to created");

lv=(ListView)view.findViewById(R.id.FeedPost);

yourAdapter adapt = new yourAdapter(getActivity().getBaseContext(),username,status);

setListAdapter(adapt);

getListView();

}

// TODO: Rename method, update argument and hook method into UI event

public void onButtonPressed(Uri uri) {

if (mListener != null) {

mListener.onFragmentInteraction(uri);

}

}

@Override

public void onAttach(Context context) {

super.onAttach(context);

if (context instanceof OnFragmentInteractionListener) {

mListener = (OnFragmentInteractionListener) context;

} else {

throw new RuntimeException(context.toString()

+ " must implement OnFragmentInteractionListener");

}

}

@Override

public void onDetach() {

super.onDetach();

mListener = null;

}

public interface OnFragmentInteractionListener {

// TODO: Update argument type and name

void onFragmentInteraction(Uri uri);

}

}

class yourAdapter extends BaseAdapter {

private static final String TAG ="mytag";

private static LayoutInflater inflater = null;

Context context;

String[] userName;

String[] status;

public yourAdapter(Context context, String[] userName,String[] status) {

// TODO Auto-generated constructor stub

this.context = context;

this.userName = userName;

this.status=status;

}

@Override

public int getCount() {

// TODO Auto-generated method stub

return userName.length;

}

@Override

public Object getItem(int position) {

// TODO Auto-generated method stub

return userName[position];

}

@Override

public long getItemId(int position) {

// TODO Auto-generated method stub

return position;

}

@Override

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

Log.i(TAG, ": made it to get view");

// TODO Auto-generated method stub

View vi = convertView;

if (vi == null)

vi = inflater.inflate(R.layout.post, null);

TextView text = (TextView) vi.findViewById(R.id.UserName);

TextView stat = (TextView) vi.findViewById(R.id.statusText);

// ImageView profile = (ImageView)vi.findViewById(R.id.ProfilePicImg);

// profile.setImageResource(R.drawable.nopic);

text.setText(userName[position]);

stat.setText(status[position]);

return vi;

}

}

The project at this state will start and display each tab.The tabs work correctly but when Code is applied to fragments class it is never called .

相关阅读:
Top