在片段中向左滑动右侧textview - Android应用

时间:2016-01-17 15:08:14

标签: android user-interface swipe swipe-gesture

请建议我在我的应用中如何实现向左或向右滑动?是否可以使用页面查看器或手势。单击项目时,我从字符串数组中获取文本视图的内容。我是应用程序开发的新手。

我的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);
    }
}

1 个答案:

答案 0 :(得分: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" />

并在定义它的视图后的代码中生成:

textView.setChecked(true);