2016-05-28 10 views
0

私は "スワイプ"プロジェクトを2つの断片の間で実現しました。私は自分の歩行メニューを使ってプロジェクトをしています。私は2つをリンクしようとしますが、私のMainActivityを含んでいるこのアセンブリコードに従うと、私のメニューはフラグメントになる必要があります。多くの機能が言語/文章を変更します。ここでフラグメント+スワイプ+ドローメニュー

there a easy idea of my plan

私のコードのMainActivityです:

package thyroid.com.thyroidmenu; 

import android.content.Intent; 
import android.net.Uri; 
import android.os.Bundle; 
import android.support.design.widget.NavigationView; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentActivity; 
import android.support.v4.widget.DrawerLayout; 
import android.support.v7.app.ActionBarDrawerToggle; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.MenuItem; 
import android.view.ViewGroup; 
import android.widget.AdapterView; 
import android.widget.Button; 
import android.widget.FrameLayout; 
import android.widget.ListView; 
import android.widget.RelativeLayout; 
import android.widget.TextView; 
import android.widget.Toast; 
import android.util.Log; 
import android.view.Menu; 


import com.google.android.gms.appindexing.Action; 
import com.google.android.gms.appindexing.AppIndex; 
import com.google.android.gms.common.api.GoogleApiClient; 


public class MainActivity extends Fragment { 
public DrawerLayout drawerLayout; 
public Toolbar toolbar; 


/** 
* ATTENTION: This was auto-generated to implement the App Indexing API. 
* See https://g.co/AppIndexing/AndroidStudio for more information. 
*/ 
public GoogleApiClient client; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 
    initNavigationDrawer(); 

    // ATTENTION: This was auto-generated to implement the App Indexing API. 
    // See https://g.co/AppIndexing/AndroidStudio for more information. 
    client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build(); 

} 

public void initNavigationDrawer() { 

    NavigationView navigationView = (NavigationView) findViewById(R.id.navigation_view); 
    assert navigationView != null; 
    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() { 
     @Override 
     public boolean onNavigationItemSelected(MenuItem menuItem) { 


      int id = menuItem.getItemId(); 

      switch (id) { 
       case R.id.home: 
        Toast.makeText(getApplicationContext(), "Home", Toast.LENGTH_SHORT).show(); 
        drawerLayout.closeDrawers(); 
        break; 
       case R.id.connection: 

        setContentView(R.layout.activity_mainlogin); 

        Intent myIntent = new Intent(MainActivity.this, login.class); 
        startActivity(myIntent); 


        drawerLayout.closeDrawers(); 
        break; 
       case R.id.settings: 
        Toast.makeText(getApplicationContext(), "Settings", Toast.LENGTH_SHORT).show(); 
        drawerLayout.closeDrawers(); 
        break; 
       case R.id.trash: 
        Toast.makeText(getApplicationContext(), "Trash", Toast.LENGTH_SHORT).show(); 
        drawerLayout.closeDrawers(); 
        break; 
       case R.id.logout: 
        finish(); 


      } 
      return true; 
     } 
    }); 
    View header = navigationView.getHeaderView(0); 
    TextView tv_email = (TextView) header.findViewById(R.id.tv_email); 
    tv_email.setText("[email protected]"); 
    drawerLayout = (DrawerLayout) findViewById(R.id.drawer); 

    ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close) { 

     @Override 
     public void onDrawerClosed(View v) { 
      super.onDrawerClosed(v); 
     } 

     @Override 
     public void onDrawerOpened(View v) { 
      super.onDrawerOpened(v); 
     } 
    }; 
    drawerLayout.addDrawerListener(actionBarDrawerToggle); 
    actionBarDrawerToggle.syncState(); 

} 

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

    // ATTENTION: This was auto-generated to implement the App Indexing API. 
    // See https://g.co/AppIndexing/AndroidStudio for more information. 
    client.connect(); 
    Action viewAction = Action.newAction(
      Action.TYPE_VIEW, // TODO: choose an action type. 
      "Main Page", // TODO: Define a title for the content shown. 
      // TODO: If you have web page content that matches this app activity's content, 
      // make sure this auto-generated web page URL is correct. 
      // Otherwise, set the URL to null. 
      Uri.parse("http://host/path"), 
      // TODO: Make sure this auto-generated app URL is correct. 
      Uri.parse("android-app://thyroid.com.thyroidmenu/http/host/path") 
    ); 
    AppIndex.AppIndexApi.start(client, viewAction); 
} 

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

    // ATTENTION: This was auto-generated to implement the App Indexing API. 
    // See https://g.co/AppIndexing/AndroidStudio for more information. 
    Action viewAction = Action.newAction(
      Action.TYPE_VIEW, // TODO: choose an action type. 
      "Main Page", // TODO: Define a title for the content shown. 
      // TODO: If you have web page content that matches this app activity's content, 
      // make sure this auto-generated web page URL is correct. 
      // Otherwise, set the URL to null. 
      Uri.parse("http://host/path"), 
      // TODO: Make sure this auto-generated app URL is correct. 
      Uri.parse("android-app://thyroid.com.thyroidmenu/http/host/path") 
    ); 
    AppIndex.AppIndexApi.end(client, viewAction); 
    client.disconnect(); 
} 


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

     View MainActivity = inflater.inflate(R.layout.activity_main, container, false); 
     ((TextView)MainActivity.findViewById(R.id.textView)).setText("iOS"); 
     return MainActivity; 
    }} 

そしてここでは、ちょうど悪い執筆しているエラーです:

MY ERRORS

どのように私は私のsetContentView/getApplicationContextを変更できますか/ findViewById ...などの断片に収まるように?

この小さなモンタージュを実現するには、断片化してプロセスを作らなければならないのですか(写真1参照)?断片は、非常に有用ないずれかの場合には実用的なようで、私なり後で...

感謝;)getActivityによって

答えて

0

てみてください()FUNCTION_NAMEを()つまり、あなたの質問に言及していた機能を、あなたのフラグメントに。しかし、それは正しい方法ではありません。引き出しのレイアウトについては、こちらの指示に従うことができます。

http://slidenerd.com/2015/07/22/android-design-support-library/

-1

あなたMainActivtyフラグメントがAppCompactActivityによってフラグメントを置き換える拡張されます。 onCreateViewメソッドをクラスの下部に配置して削除すると、そのメソッドは必要ありません。あなたは、コードの行の下に必要がある場合:あなたのonCreateView方法で述べた

((TextView)MainActivity.findViewById(R.id.textView)).setText("iOS");

あなたはのonCreateメソッドにコード行を移動することができます。しかし、あなたはそれにのonCreate方法で次のように記述しています

((TextView)findViewById(R.id.textView)).setText("iOS");

コードの何も他のラインはようにメソッドを削除onCreateView方法から必要とされます。

+0

こんにちは、あなたの答えを教えてください。 "AppCompactActivity"では、私はまだこのエラーがあります: "エラー:(21、24)エラー:互換性のないタイプ:MainActivityをフラグメントに変換できません"。だから私は "FragmentActivity"を試しましたが、これを取得しました: "エラー:(21、24)エラー:互換性のないタイプ:MainActivityをフラグメントに変換できません" ...任意のアイデア? – zouarv42

+0

@ zouarv42の 'public class MainActivity extends Fragment'を' public class MainActivity extends AppCompatActivity'に置き換えると、あなたが受け取ったエラーログのイメージを投稿できます。変更後の最新のコード。 –

+0

変更あり:) – zouarv42

0

あり、私が修正した後に得られたものを:

package thyroid.com.thyroidmenu; 

import android.content.Intent; 
import android.net.Uri; 
import android.os.Bundle; 
import android.support.design.widget.NavigationView; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentActivity; 
import android.support.v4.widget.DrawerLayout; 
import android.support.v7.app.ActionBarDrawerToggle; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.MenuItem; 
import android.view.ViewGroup; 
import android.widget.AdapterView; 
import android.widget.Button; 
import android.widget.FrameLayout; 
import android.widget.ListView; 
import android.widget.RelativeLayout; 
import android.widget.TextView; 
import android.widget.Toast; 
import android.util.Log; 
import android.view.Menu; 


import com.google.android.gms.appindexing.Action; 
import com.google.android.gms.appindexing.AppIndex; 
import com.google.android.gms.common.api.GoogleApiClient; 


public class MainActivity extends AppCompatActivity { 
    public DrawerLayout drawerLayout; 
    public Toolbar toolbar; 


/** 
* ATTENTION: This was auto-generated to implement the App Indexing API. 
* See https://g.co/AppIndexing/AndroidStudio for more information. 
*/ 
public GoogleApiClient client; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 
    initNavigationDrawer(); 

    // ATTENTION: This was auto-generated to implement the App Indexing API. 
    // See https://g.co/AppIndexing/AndroidStudio for more information. 
    client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build(); 

} 

public void initNavigationDrawer() { 

    NavigationView navigationView = (NavigationView) findViewById(R.id.navigation_view); 
    assert navigationView != null; 
    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() { 
     @Override 
     public boolean onNavigationItemSelected(MenuItem menuItem) { 


      int id = menuItem.getItemId(); 

      switch (id) { 
       case R.id.home: 
        Toast.makeText(getApplicationContext(), "Home", Toast.LENGTH_SHORT).show(); 
        drawerLayout.closeDrawers(); 
        break; 
       case R.id.connection: 

        setContentView(R.layout.activity_mainlogin); 

        Intent myIntent = new Intent(MainActivity.this, login.class); 
        startActivity(myIntent); 


        drawerLayout.closeDrawers(); 
        break; 
       case R.id.settings: 
        Toast.makeText(getApplicationContext(), "Settings", Toast.LENGTH_SHORT).show(); 
        drawerLayout.closeDrawers(); 
        break; 
       case R.id.trash: 
        Toast.makeText(getApplicationContext(), "Trash", Toast.LENGTH_SHORT).show(); 
        drawerLayout.closeDrawers(); 
        break; 
       case R.id.logout: 



      } 
      return true; 
     } 
    }); 
    View header = navigationView.getHeaderView(0); 
    TextView tv_email = (TextView) header.findViewById(R.id.tv_email); 
    tv_email.setText("[email protected]"); 
    drawerLayout = (DrawerLayout) findViewById(R.id.drawer); 

    ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close) { 

     @Override 
     public void onDrawerClosed(View v) { 
      super.onDrawerClosed(v); 
     } 

     @Override 
     public void onDrawerOpened(View v) { 
      super.onDrawerOpened(v); 
     } 
    }; 
    drawerLayout.addDrawerListener(actionBarDrawerToggle); 
    actionBarDrawerToggle.syncState(); 

} 

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

    // ATTENTION: This was auto-generated to implement the App Indexing API. 
    // See https://g.co/AppIndexing/AndroidStudio for more information. 
    client.connect(); 
    Action viewAction = Action.newAction(
      Action.TYPE_VIEW, // TODO: choose an action type. 
      "Main Page", // TODO: Define a title for the content shown. 
      // TODO: If you have web page content that matches this app activity's content, 
      // make sure this auto-generated web page URL is correct. 
      // Otherwise, set the URL to null. 
      Uri.parse("http://host/path"), 
      // TODO: Make sure this auto-generated app URL is correct. 
      Uri.parse("android-app://thyroid.com.thyroidmenu/http/host/path") 
    ); 
    AppIndex.AppIndexApi.start(client, viewAction); 
} 

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

    // ATTENTION: This was auto-generated to implement the App Indexing API. 
    // See https://g.co/AppIndexing/AndroidStudio for more information. 
    Action viewAction = Action.newAction(
      Action.TYPE_VIEW, // TODO: choose an action type. 
      "Main Page", // TODO: Define a title for the content shown. 
      // TODO: If you have web page content that matches this app activity's content, 
      // make sure this auto-generated web page URL is correct. 
      // Otherwise, set the URL to null. 
      Uri.parse("http://host/path"), 
      // TODO: Make sure this auto-generated app URL is correct. 
      Uri.parse("android-app://thyroid.com.thyroidmenu/http/host/path") 
    ); 
    AppIndex.AppIndexApi.end(client, viewAction); 
    client.disconnect(); 
} 
} 

スワイプを実現コード:

package thyroid.com.thyroidmenu; 

import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentStatePagerAdapter; 

public class TabPagerAdapter extends FragmentStatePagerAdapter { 
public TabPagerAdapter(FragmentManager fm) { 
    super(fm); 
    // TODO Auto-generated constructor stub 
} 

@Override 
public Fragment getItem(int i) { 
    switch (i) { 
     case 0: 
      //Fragment for Android Tab 
      return new Windows(); 
     case 1: 
      //Fragment for Ios Tab 
      return new MainActivity(); 
       } 
    return null; 

} 

@Override 
public int getCount() { 
    // TODO Auto-generated method stub 
    return 3; //No of Tabs 
} 


} 

とエラーは次のとおりです。

​​

私はいけないと私のtoolbarmenu/drawermenuに "fragment"を使うとうまくいきません。 私はおそらく別のクラスであるメニューを分離しようとする必要がありますし、フラグメントでそれを呼び出す。どのように?...;)