2014-01-20 90 views
0

我编写了一个带有片段的Android应用程序。一个ListView片段和一个细节片段。 我想要做的是,如果有人点击细节活动内部,“View.Gone”的布局应该是“View.Visible”。该代码工作没有错误,但屏幕上没有任何更改。 您可以在ImageButton btn上的clik事件所在的细节代码中看到它。 我错了什么?更新细节布局的片段

什么是更新详细信息屏幕的最佳方法?如果有人有一个小例子或者可以写我在哪里,在我的代码我必须要改变什么,这让我很开心:-) 非常感谢 汤姆

的FragmentActivity:

public class CacheFragment extends SherlockFragmentActivity { 
CacheListFragment f; 

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.list_cachelist); 

    f = new CacheListFragment(); 
    // Supply index input as an argument. 
    Bundle args = new Bundle(); 
    f.setArguments(args); 
} 

下面是详细信息片段,在这里你可以看到,如果有人点击ImageButton的热点问题研究应该发生:

public class CacheDetailsFragment extends SherlockFragment implements OnClickListener { 
    private CacheDetailsLoading cdLoad= new CacheDetailsLoading(); 
    private static GeocacheDetails _cacheDetails = new GeocacheDetails(); 
    private static GCRatingTyp _cacheVote = new GCRatingTyp(); 
    private CacheDetailsUsing cdUsing = new CacheDetailsUsing(); 
    private Activity _context; 
    private static CacheDetailsFragment f; 
    private View view; 
    /** 
    * Create a new instance of DetailsFragment, initialized to 
    * show the text at 'index'. 
    */ 
    public static CacheDetailsFragment newInstance(int index) { 
     f = new CacheDetailsFragment(); 
     // Supply index input as an argument. 
     Bundle args = new Bundle(); 
     args.putInt("index", index); 
     f.setArguments(args); 
     return f; 
    } 
    public int getShownIndex() { 
     return getArguments().getInt("index", 0); 
    } 
    public void setCacheDetail(GeocacheDetails cacheDetails) 
    { 
    _cacheDetails = cacheDetails; 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    if (container == null) { 

     return null; 
    } 


    Bundle bundle=getArguments(); 
    _cacheVote= bundle.getParcelable("cacheVote"); 
    int index = bundle.getInt("index"); 
    _cacheDetails=StaticCacheListByGroup.getCacheList().get(index); 

    _context = getActivity(); 
    _context.setTheme(R.style.Theme_Sherlock_Light_DarkActionBar); 

      view = inflater.inflate(R.layout.list_cachedetails, container,false); 
      ((RelativeLayout) view.findViewById(R.id.relativeLoggingInfo)).setVisibility(View.GONE); 
      ((RelativeLayout) view.findViewById(R.id.relativeKategorienInfo)).setVisibility(View.GONE); 


     ImageButton btn = (ImageButton) view.findViewById(R.id.description_expand); 
     btn.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) 
     { 
      if(((RelativeLayout) getActivity().findViewById(R.id.relativeDescriptionInfo)).getVisibility() == View.GONE) 
      { 
        ((ImageButton) getActivity().findViewById(R.id.description_expand)).setBackgroundResource(R.drawable.navigation_collapse_dark); 
        ((RelativeLayout) getActivity().findViewById(R.id.relativeDescriptionInfo)).setVisibility(View.VISIBLE); 

       } 
       else 
       { 
        ((ImageButton) getActivity().findViewById(R.id.description_expand)).setBackgroundResource(R.drawable.navigation_expand_dark); 
        ((RelativeLayout) getActivity().findViewById(R.id.relativeDescriptionInfo)).setVisibility(View.GONE); 
       }     
     });  
     return view; 
    } 

} 

现在Listfragment:

public class CacheListFragment extends SherlockListFragment { 
boolean isDualPane; 
int mCurCheckPosition = 0; 
private CacheListArrayAdapter _adapter; 
private SharedPrefs _sp= new SharedPrefs(); 
private double latitude=0; 
private double longitude=0; 


@Override 
public void onActivityCreated(Bundle savedInstanceState) { 
    super.onActivityCreated(savedInstanceState); 



    latitude =Double.parseDouble(_sp.getSharedPrefs(getActivity(), LibraryDefaults.PROGRAMMNAME, "Latitude", "0")); 
    longitude =Double.parseDouble(_sp.getSharedPrefs(getActivity(), LibraryDefaults.PROGRAMMNAME, "Longitude", "0")); 


    // Check to see if we have a frame in which to embed the details 
    // fragment directly in the containing UI. 
    View detailsFrame = getActivity().findViewById(R.id.details); 
    isDualPane = detailsFrame != null && detailsFrame.getVisibility() == View.VISIBLE; 

    if(!isDualPane) 
    { 
     Bundle bundle = getActivity().getIntent().getExtras(); 
     if(bundle != null && bundle.containsKey("Titel")) 
      ((TextView) getActivity().findViewById(R.id.listtitle)).setText(bundle.getString("Titel")); 
     else 
      ((TextView) getActivity().findViewById(R.id.listtitle)).setText(this.getResources().getString(R.string.caches_listtitle)); 

    } 

    if (StaticCacheListByGroup.getCacheList() != null) 
    { 
     GeocachingCompass gc = new GeocachingCompass(getActivity()); 
     _adapter = new CacheListArrayAdapter(getActivity(), StaticCacheListByGroup.getCacheList(), longitude,latitude); 
     _adapter.setActualCoordinates(new LatLng(latitude,longitude)); 
     _adapter.setActualHeading(gc.getBearing(latitude,longitude)); 
     if (_adapter != null) 
      setListAdapter(_adapter); 

     if (savedInstanceState != null) { 
      // Restore last state for checked position. 
      mCurCheckPosition = savedInstanceState.getInt("curChoice", 0); 
     } 
     getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE); 
     getListView().setSoundEffectsEnabled(true); 
     getListView().setSmoothScrollbarEnabled(true); 
     getListView().setDrawSelectorOnTop(false); 
     getListView().setCacheColorHint(R.color.transparentBlack); 
     getListView().setDivider(getResources().getDrawable(R.color.divider)); 
     getListView().setDividerHeight(5); 



     if (isDualPane) { 
      // In dual-pane mode, the list view highlights the selected item. 
      getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE); 

      showDetails(mCurCheckPosition); 
     } 
    } 

    } 

@Override 
public void onResume() { 

    super.onResume(); 
    GeocachingCompass gc = new GeocachingCompass(getActivity()); 
    _adapter = new CacheListArrayAdapter(getActivity(), StaticCacheListByGroup.getCacheList(), longitude,latitude); 
    _adapter.setActualCoordinates(new LatLng(latitude,longitude)); 
    _adapter.setActualHeading(gc.getBearing(latitude,longitude)); 
    if (_adapter != null) 
     setListAdapter(_adapter); 

} 

@Override 
public void onSaveInstanceState(Bundle outState) { 
    super.onSaveInstanceState(outState); 
    outState.putInt("curChoice", mCurCheckPosition); 
} 

@Override 
public void onListItemClick(ListView l, View v, int position, long id) { 
    showDetails(position); 
} 

/** 
* Helper function to show the details of a selected item, either by 
* displaying a fragment in-place in the current UI, or starting a 
* whole new activity in which it is displayed. 
*/ 
void showDetails(int index) { 
    mCurCheckPosition = index; 

    ReadGCVote getVote = new ReadGCVote(); 
    GeocacheDetails cacheDetails = new GeocacheDetails(); 
    cacheDetails=StaticCacheListByGroup.getCacheList().get(index); 


    if (isDualPane) { 
     // We can display everything in-place with fragments, so update 
     // the list to highlight the selected item and show the data. 
     getListView().setItemChecked(index, true); 

     // Check what fragment is currently shown, replace if needed. 
     CacheDetailsFragment details = (CacheDetailsFragment) 
       getActivity().getSupportFragmentManager().findFragmentById(R.id.details); 
     if (details == null || details.getShownIndex() != index) { 
      // Make new fragment to show this selection. 
      details = CacheDetailsFragment.newInstance(index); 

      // Execute a transaction, replacing any existing fragment 
      // with this one inside the frame. 
      FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction(); 
      ft.replace(R.id.details, details); 
      ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); 
      ft.commit(); 
     } 

    } else { 

     // Otherwise we need to launch a new activity to display 
     Intent intent = new Intent(); 
     intent.setClass(getActivity(), CacheDetailsActivity.class); 
     intent.putExtra("index", index); 
     intent.putExtra("cacheDetails",cacheDetails); 
     intent.putExtra("cacheVote",getVote.getGCVoteByCacheGuid(StaticGCVoteList.getCacheList(), cacheDetails.GetGUID())); 
     startActivity(intent); 
    } 
} 

} 

回答

0

我发现:-)

在细节片段的代码片段的bug ......

public void onClick(View v) 
    { 
     if(((RelativeLayout) getActivity().findViewById(R.id.relativeDescriptionInfo)).getVisibility() == View.GONE) 
    } 

...你不应该使用 “getActivity()” 使用 “视图” from“view = inflater.inflate(R.layout.list_cachedetails,container,false);”

然后它会工作