2012-11-21 69 views
1

在一個片段中有兩個片段。我在左邊和右邊的片段中有一個列表,在ListFragment中顯示ListView。如果我在ListView中單擊列表,我需要顯示另一個視圖來代替ListView,但是左側的片段仍然存在。在項目選擇列表片段而不是活動上替換片段活動

在onclick事件中,我在做意圖,但整個視圖正在改變。相反,我只需要更改列表視圖。

public class FriendsFragment extends ListFragment{ 
    public static String[] friendsaddoptions=new String[]{ 
    "Fox User","FaceBook","Email","Twiter","SMS" 
    }; 
    private Intent listIntent = null; 
    ListView listview; 
    Friends_Names_List_Adapter friendsnameslistadapter; 
    JSONArray dataJsonObject; 
    Button add; 
    Button back; 
    Game_Sections_Details_Adapter GameSectionsDetails; 
    View view; 
    Context context = getActivity(); 
    @Override 
    public View onCreateView(LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) { 
    View view=inflater.inflate(R.layout.friends_list, container,false); 
    listview=(ListView)view.findViewById(android.R.id.list); 
    add=(Button) view.findViewById(R.id.button_add); 
    back=(Button)view. findViewById(R.id.button_back); 
    back.setVisibility(View.INVISIBLE); 
    FriendsFragmentData dataobserver=new FriendsFragmentData(); 
    ServerManager.getInstance().addObserver(dataobserver); 
    ServerManager.getInstance().readLoopForFriends_Names_List(AppConstances.Friends_Names_List); 
    if(friendsnameslistadapter==null) 
     friendsnameslistadapter=new Friends_Names_List_Adapter(getActivity(), new JSONArray()); 
    listview.setAdapter(friendsnameslistadapter); 
    add.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { /*Intent in=new 
     Intent(getActivity().getBaseContext(), FriendsNamesAddActivity.class); 
     startActivity(in);*/ 
     ArrayAdapter< String> adapter=new ArrayAdapter<String>(getActivity().getBaseContext(), 
     R.layout.friends_add_listitem,R.id.friends_add_option,friendsaddoptions); 
     listview.setAdapter(adapter); 
     if(adapter!=null) 
     back.setVisibility(View.VISIBLE); 
     listview.setOnItemClickListener(new OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> arg0, View view, int position, long id) { 
      Intent intent = new Intent(getActivity().getBaseContext(),FoxUserActivity.class); 
      startActivity(intent); 
     } 
     }); 
     if(adapter!=null) { 
     add.setVisibility(View.INVISIBLE); 
     } 
    } 
    }); 
    back.setOnClickListener(new OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     listview.setAdapter(friendsnameslistadapter); 
     add.setVisibility(View.VISIBLE); 
     back.setVisibility(View.INVISIBLE); 
    } 
    }); 
    return view; 
} 

而這個類顯示後的onclick」

public class FoxUserActivity extends FragmentActivity{ 
    public static TabHost tHost; 
    Button add,back; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.foxuser); 
    add=(Button) findViewById(R.id.button_add); 
    back=(Button) findViewById(R.id.button_back); 
    add.setVisibility(View.INVISIBLE); 
    back.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
     onBackPressed(); 
     } 
    }); 
    } 
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.activity_game, menu); 
    return true; 
    }} 

我希望有人能幫助我。在此先感謝。

+1

請刪除未知代碼並添加一些空格以獲得更多可讀性 –

回答

0

哦,你的代碼是可怕的。我不認爲有人希望閱讀它,無論如何,我只是閱讀它,而且我看到你使用了一個ListView和2 Adapter,如果你使用替換的Adapter來改變你的ListView,我不認爲它不好,而應該使用2個獨立的ListFragment,你可以用另一個替換。如果你不知道如何去做在。請閱讀this post