2016-01-17 53 views
0

请向我推荐在应用中左右滑动的实施方式?是否可以使用页面查看器或手势。当点击项目时,我从字符串数组中获取文本视图的内容。我对应用程序开发不熟悉。向左滑动向右滑动片段中的textview - Android应用

我的MainActivity XML

import android.app.FragmentTransaction; 
import android.graphics.Color; 
import android.os.Bundle; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.widget.DrawerLayout; 
import android.support.v7.app.ActionBar; 
import android.support.v7.app.ActionBarDrawerToggle; 
import android.support.v7.app.AppCompatActivity; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 
import android.widget.FrameLayout; 
import android.widget.ListView; 
import android.widget.TextView; 


public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener 
{ 
    private ActionBarDrawerToggle actionBarDrawerToggle; 
    private DrawerLayout drawerLayout; 
    private ListView navList; 
    private FragmentManager fragmentManager; 




    boolean nightmode=false; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     drawerLayout = (DrawerLayout)findViewById(R.id.drawerlayout); 

     navList = (ListView)findViewById(R.id.navlist); 

     String[] versionName = getResources().getStringArray(R.array.version_names); 

     navList.setChoiceMode(ListView.CHOICE_MODE_SINGLE); 

     ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, versionName); 
     navList.setAdapter(adapter); 

     navList.setOnItemClickListener(this); 

     actionBarDrawerToggle = new ActionBarDrawerToggle(this,drawerLayout,R.string.opendrawer,R.string.closedrawer); 
     drawerLayout.setDrawerListener(actionBarDrawerToggle); 

     ActionBar actionBar = getSupportActionBar(); 
     actionBar.setDisplayShowHomeEnabled(true); 
     actionBar.setDisplayHomeAsUpEnabled(true); 
     fragmentManager = getSupportFragmentManager(); 

     OnSelectionChanged(0); 


    } 


    public void OnSelectionChanged(int position) { 
     DescriptionFragment descriptionFragment = (DescriptionFragment) getFragmentManager() 
       .findFragmentById(R.id.description_fragment); 

     if (descriptionFragment != null){ 
      // If description is available, we are in two pane layout 
      // so we call the method in DescriptionFragment to update its content 
      descriptionFragment.setDescription(position); 
     } else { 
      DescriptionFragment newDesriptionFragment = new DescriptionFragment(); 
      Bundle args = new Bundle(); 

      args.putInt(DescriptionFragment.KEY_POSITION,position); 
      newDesriptionFragment.setArguments(args); 
      FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction(); 

      // Replace whatever is in the fragment_container view with this fragment, 
      // and add the transaction to the backStack so the User can navigate back 
      fragmentTransaction.replace(R.id.fragment_container,newDesriptionFragment); 
      fragmentTransaction.addToBackStack(null); 
      fragmentTransaction.commit(); 
     } 
    } 

    @Override 
    protected void onPostCreate(Bundle savedInstanceState) { 
     super.onPostCreate(savedInstanceState); 
     actionBarDrawerToggle.syncState(); 
    } 

    @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_main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 

     TextView textElement = (TextView) findViewById(R.id.version_description); 
     FrameLayout mainLayout = (FrameLayout) findViewById(R.id.fragment_container); 
     if(nightmode) textElement.setTextColor(Color.WHITE); 

     switch(item.getItemId()){ 
      case R.id.action_settings: 
       if (nightmode) { 
        mainLayout.setBackgroundResource(R.color.white); 

        textElement.setTextColor(Color.BLACK); 
        nightmode=false; 
       }else { 
        mainLayout.setBackgroundResource(R.color.background_color); 
        textElement.setTextColor(Color.WHITE); 
        nightmode=true; 
       } 
       break; 
      case android.R.id.home: 
       if (drawerLayout.isDrawerOpen(navList)){ 
        drawerLayout.closeDrawer(navList); 
       }else{ 
        drawerLayout.openDrawer(navList); 
       } 
       break; 
      case R.id.action_share: 

       break; 


     } 

     return super.onOptionsItemSelected(item); 
    } 

    @Override 
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
     OnSelectionChanged(position); 
     drawerLayout.closeDrawer(navList); 
    } 

} 

我DescriptionFragment

import android.app.Fragment; 
import android.app.FragmentManager; 
import android.app.FragmentTransaction; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.TextView; 

/** 
* Created by sathi on 16-01-2016. 
*/ 
public class DescriptionFragment extends Fragment { 

    final static String KEY_POSITION = "position"; 
    int mCurrentPosition = -1; 

    String[] mVersionDescriptions; 
    TextView mVersionDescriptionTextView; 

    public DescriptionFragment(){ 

    } 

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

     mVersionDescriptions = getResources().getStringArray(R.array.version_descriptions); 

    } 

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

     /* DescriptionFragment descriptionFragment = new DescriptionFragment(); 
     Object fromFragment = null; 

     Object toFragment=null; 
     descriptionFragment.addFragment(Fragment fromFragment, Fragment toFragment);*/ 


     // If the Activity is recreated, the savedInstanceStare Bundle isn't empty 
     // we restore the previous version name selection set by the Bundle. 
     // This is necessary when in two pane layout 
     if (savedInstanceState != null) { 
      mCurrentPosition = savedInstanceState.getInt(KEY_POSITION); 
     } 

     // FragmentTransaction fragmentTransaction = null; 
     // fragmentTransaction.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_left); 

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

     mVersionDescriptionTextView = (TextView) view.findViewById(R.id.version_description); 
     return view; 

    /* DescriptionFragment fragment1 = new DescriptionFragment(); 
     (getSupportFragmentManager().beginTransaction().add(R.id.description_fragment, fragment1) 
       .add(R.id.description_fragment, fragment1).commit()){ 

     }*/ 






    } 

    public void addFragment(Fragment fromFragment, Fragment toFragment) { 
     FragmentManager manager = getFragmentManager(); 
     FragmentTransaction transaction = manager.beginTransaction(); 
     transaction.add(R.id.fragment_container,toFragment, toFragment.getClass().getName()); 
     transaction.hide(fromFragment); 
     transaction.addToBackStack(toFragment.getClass().getName()); 
     transaction.commit(); 
    } 

    public void replaceFragment(Fragment fromFragment, Fragment toFragment) { 
     FragmentManager manager = getFragmentManager(); 
     FragmentTransaction transaction = manager.beginTransaction(); 
     transaction.replace(R.id.fragment_container,toFragment, toFragment.getClass().getName()); 
     transaction.hide(fromFragment); 
     transaction.addToBackStack(toFragment.getClass().getName()); 
     transaction.commit(); 
    } 

    private FragmentManager getSupportFragmentManager() { 
     return null; 
    } 


    @Override 
    public void onStart() { 
     super.onStart(); 

     // During the startup, we check if there are any arguments passed to the fragment. 
     // onStart() is a good place to do this because the layout has already been 
     // applied to the fragment at this point so we can safely call the method below 
     // that sets the description text 
     Bundle args = getArguments(); 
     if (args != null){ 
      // Set description based on argument passed in 
      setDescription(args.getInt(KEY_POSITION)); 
     } else if(mCurrentPosition != -1){ 
      // Set description based on savedInstanceState defined during onCreateView() 
      setDescription(mCurrentPosition); 
     } 
    } 

    public void setDescription(int descriptionIndex){ 
     mVersionDescriptionTextView.setText(mVersionDescriptions[descriptionIndex]); 
     mCurrentPosition = descriptionIndex; 
    } 

    @Override 
    public void onSaveInstanceState(Bundle outState) { 
     super.onSaveInstanceState(outState); 

     // Save the current description selection in case we need to recreate the fragment 
     outState.putInt(KEY_POSITION,mCurrentPosition); 
    } 
} 
+0

我不知道如果我清楚地理解你想说什么,但ViewPager是一种方便的方式来实施滑动手势 – drWisdom

回答

0

我没有,但如果你在一行自动希望TextView举动明白,如果实际上你要做我的理解以显示它的其余部分:

<TextView 
     android:id="@+id/textView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:ellipsize="marquee" 
     android:focusable="true" 
     android:focusableInTouchMode="true" 
     android:freezesText="true" 
     android:marqueeRepeatLimit="marquee_forever" 
     android:scrollHorizontally="true" 
     android:singleLine="true" /> 

和i n代码后定义它的视图使这个:

textView.setChecked(true);