액티비티를 호출 할 때


Intent intent = new Intent(MainActivity.this, SubActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.anim_slide_in_right, R.anim.anim_slide_out_left);



액티비티를 종료할 때


@Override
public void finish() {
super.finish();
if(isChoice){
overridePendingTransition(R.anim.anim_slide_in_right, R.anim.anim_slide_out_left);
}else{
overridePendingTransition(R.anim.anim_slide_in_left, R.anim.anim_slide_out_right);
}
}

anim 파일들은 해당경로에 넣어주세요


res/anim/여기에 밑에 파일들을 넣어주세요.



anim_slide_in_left.xml

anim_slide_in_right.xml

anim_slide_out_left.xml

anim_slide_out_right.xml


728x90
반응형

먼저 RecyclerView를 만들기 위해서 



앱 build.gradle에 appcompatV7와 recycleview lib를 추가해야합니다.


compile 'com.android.support:appcompat-v7:25.0.0'
compile 'com.android.support:recyclerview-v7:25.0.0'



MainActivity


import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import com.damoa.testfragment.dummy.DummyContent;

public class MainActivity extends AppCompatActivity {

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

RecyclerView recyclerView = (RecyclerView) findViewById(R.id.test_recycler);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(new MyItemRecyclerViewAdapter(this,DummyContent.ITEMS));
}

}



MyItemRecyclerViewAdapter


import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import com.damoa.testfragment.dummy.DummyContent.DummyItem;

import java.util.List;


public class MyItemRecyclerViewAdapter extends RecyclerView.Adapter<MyItemRecyclerViewAdapter.ViewHolder> {

private final List<DummyItem> mValues;
private Context mContext = null;

public MyItemRecyclerViewAdapter(Context context, List<DummyItem> items) {
mContext = context;
mValues = items;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.recycler_item, parent, false);
return new ViewHolder(view);
}

@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
holder.mTitle.setText(mValues.get(position).title);

holder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(mContext,"test"+position,Toast.LENGTH_SHORT).show();
}
});
}

@Override
public int getItemCount() {
return mValues.size();
}

public class ViewHolder extends RecyclerView.ViewHolder {
public final View mView;
public final TextView mTitle;

public ViewHolder(View view) {
super(view);
mView = view;
mTitle = (TextView) view.findViewById(R.id.item_title);
}

}
}



DummyContent


import java.util.ArrayList;
import java.util.List;

/**
* Helper class for providing sample content for user interfaces created by
* Android template wizards.
* <p>
* TODO: Replace all uses of this class before publishing your app.
*/
public class DummyContent {

/**
* An array of sample (dummy) items.
*/
public static final List<DummyItem> ITEMS = new ArrayList<DummyItem>();


private static final int COUNT = 25;

static {
// Add some sample items.
for (int i = 1; i <= COUNT; i++) {
addItem(createDummyItem(i));
}
}

private static void addItem(DummyItem item) {
ITEMS.add(item);
}

private static DummyItem createDummyItem(int position) {
return new DummyItem(String.valueOf(position)+"Item "+ position);
}

private static String makeDetails(int position) {
StringBuilder builder = new StringBuilder();
builder.append("Details about Item: ").append(position);
for (int i = 0; i < position; i++) {
builder.append("\nMore details information here.");
}
return builder.toString();
}

/**
* A dummy item representing a piece of content.
*/
public static class DummyItem {
public final String title;

public DummyItem(String id) {
this.title = id;
}
}
}



activity_main.xml


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.damoa.testfragment.MainActivity">

<android.support.v7.widget.RecyclerView
android:id="@+id/test_recycler"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>



recycler_item.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:id="@+id/item_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/text_margin"
android:textAppearance="?attr/textAppearanceListItem" />

</LinearLayout>



728x90
반응형

MainActiviy


import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

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

FragmentManager fm = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.fragment_test, ItemFragment.newInstance(5));
fragmentTransaction.commit();
}
}


activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.damoa.testfragment.MainActivity">

<LinearLayout
android:id="@+id/fragment_test"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"

/>
</RelativeLayout>



ItemFragment

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class ItemFragment extends Fragment {

private static final String ARG_COLUMN_COUNT = "column-count";
private int mColumnCount = 0; //액티비티 와 프레그먼트 데이터 전달 예제

/**
* 액티비티 -> 프래그먼트 데이터 전달 예제
* @param columnCount
* @return
*/
public static ItemFragment newInstance(int columnCount) {
ItemFragment fragment = new ItemFragment();
Bundle args = new Bundle();
args.putInt(ARG_COLUMN_COUNT, columnCount);
fragment.setArguments(args);
return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 액티비티 -> 프래그먼트 데이터 전달 예제
if (getArguments() != null) {
mColumnCount = getArguments().getInt(ARG_COLUMN_COUNT);
}
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_item_list, container, false);
return view;
}

}



728x90
반응형

Fragment를 만들다 보면 간혹가다 이런 오류를 만날 수 있다.



보통 이런 경우는 


Fragment를 호출하는 Activity에서는 android.app.FragmentManager를 사용하고 있는데 


Fragment는 android.support.v4.app.Fragment;로 import 되어 있는 경우이거나 반대일 가능성이 크다.



1. V4 라이브러리를 쓸 경우


Fragment에 v4를 import 해주고

import android.support.v4.app.Fragment;
public class ItemFragment extends Fragment {}

Fragment를 사용할 액티비에서는 다음과같이 정의해주면 된다.

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

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

FragmentManager fm = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.fragment_test, ItemFragment.newInstance("d"));
fragmentTransaction.commit();
}
}

2. V4를 쓰지 않을 경우


Fragment import를 다음과 같이 해주고

import android.app.Fragment;
public class ItemFragment extends Fragment {}

Fragment를 사용할 액티비에서는 다음과같이 정의해주면 된다.


import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

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

FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.fragment_test, ItemFragment.newInstance("d"));
fragmentTransaction.commit();
}
}


728x90
반응형

소스 다운받는곳

https://github.com/tlshenm/SearchToolbarTest


----------------------------------------------------------CODE-------------------------------------------------

 

SearchActivity


 

public class SearchActivity extends AppCompatActivity {

private NoticeRecyclerAdapter mAdapter;
private NoticeContent mNoticeContent;

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

private void initView() {
getIntentDate();
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.notice_search_recycler);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
mAdapter = new NoticeRecyclerAdapter(this, mNoticeContent.getNoticeList()); //new NoticeRecyclerAdapter(Context, 당신의 데이터 리스트)
recyclerView.setAdapter(mAdapter);
}

private void getIntentDate(){
Intent intent = getIntent();
mNoticeContent =(NoticeContent)intent.getExtras().getSerializable(NoticeContent.NOTICE_CONTENT);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

SearchManager searchManager = (SearchManager) this.getSystemService(Context.SEARCH_SERVICE);
searchView = (SearchView) findViewById(R.id.notice_search_view);
searchView.onActionViewExpanded(); //바로 검색 할 수 있도록

if (searchView != null) {
searchView.setSearchableInfo(searchManager.getSearchableInfo(this.getComponentName()));
searchView.setQueryHint(getString(R.string.notice_search_hint));
queryTextListener = new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextChange(String newText) {
Log.i("onQueryTextChange", newText);

mAdapter.setFilter(filter(mNoticeContent.getNoticeList(), newText));
return true;
}

@Override
public boolean onQueryTextSubmit(String query) {
Log.i("onQueryTextSubmit", query);

return true;
}
};
searchView.setOnQueryTextListener(queryTextListener);
}

return true;
}


private List<NoticeItem> filter(List<NoticeItem> noticeListItem, String query) {
query = query.toLowerCase();

final List<NoticeItem> filteredNoticeList = new ArrayList<>();
if (query != null && !query.equals("")) {
for (NoticeItem model : noticeListItem) {
final String title = model.getTitle().toLowerCase();
final String name = model.getName().toLowerCase();
if (title.contains(query)) {
filteredNoticeList.add(model);
}else if(name.contains(query)){
filteredNoticeList.add(model);
}
}
}
return filteredNoticeList;
}

}

---------------------------------------------------------------------------------------------------------------------

 

NoticeRecyclerAdapter

 

public class NoticeRecyclerAdapter extends RecyclerView.Adapter<NoticeRecyclerAdapter.ViewHolder> {

private final List<NoticeItem> mValues;
private Context mContext = null;

public NoticeRecyclerAdapter(Context context, List<NoticeItem> items) {
this.mContext = context;
mValues = items;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.notice_item, parent, false);
return new ViewHolder(view);
}


@Override
public int getItemCount() {
return mValues.size();
}

//이부분 중요!! 검색 리스트를 나오게하기 위해 꼭 필요
public void setFilter(List<NoticeItem> items) {
mValues.clear();
mValues.addAll(items);
notifyDataSetChanged();
}

@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
holder.mItem = mValues.get(position);
holder.mTitleText.setText(mValues.get(position).getTitle());
holder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}


public class ViewHolder extends RecyclerView.ViewHolder {
public final View mView;
public final TextView mTitleText;

public NoticeItem mItem;

public ViewHolder(View view) {
super(view);
mView = view;
mTitleText = (TextView) view.findViewById(R.id.notice_item_title);
}
}
}

 


 

----------------------------------------------------------XML---------------------------------------------------

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">




<android.support.v7.widget.Toolbar
android:id="@+id/notice_search_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_alignParentTop="true"
android:background="@color/white_color"
android:elevation="5dp"
app:contentInsetStartWithNavigation="0dp">




<android.support.v7.widget.SearchView
android:id="@+id/notice_search_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>


</android.support.v7.widget.Toolbar>



<android.support.v7.widget.RecyclerView
android:id="@+id/notice_search_recycler"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/notice_search_toolbar"/>



</RelativeLayout>


 

 


728x90
반응형

'Android' 카테고리의 다른 글

RecyclerView 만들기  (0) 2016.11.14
Activity와 Fragment간 데이터 전달  (0) 2016.11.14
android SHA1 알아내기  (0) 2016.10.19
Android TextView Line 가져오기  (0) 2016.09.21
안드로이드 StatusBar 높이 알아내기  (0) 2016.07.08
흔히 googleApi를 사용하기 전에 sha-1지문이 필요한데요


window에서는 Android studio를 설치 한뒤


Windows

keytool -list -v -keystore ~/.android/debug.keystore -alias androiddebugkey -storepass android -keypass android


Android studio 터미널에서 위에 지문을 입력해주면 되는데요


저 같은경우에는 debug.keystore의 경로를 찾아내서 다음과 같이 입력했습니다.


keytool -list -v -keystore C:/Users/Administrator/.android/debug.keystore -alias androiddebugkey -storepass android -keypass android




위와 같은 과정을 거치면 SHA-1이 똬악~~!



OS X, Linux

keytool -list -v -keystore "%USERPROFILE%\.android\debug.keystore" -alias androiddebugkey -storepass android -keypass android



728x90
반응형
int android.widget.TextView.getLineCount()


Return the number of lines of text, or 0 if the internal Layout has not been built.
-내부 레이아웃이 내장되어 있지 않은 경우,텍스트 줄의 수를 돌려줍니다


getLineCount()메서드로 Parent View가 넓어지거나 줄어들어도 해당 메서드로 TextView글자 라인수를 가져 올 수 있습니다.


예를들어
--------------------------------------------------------------------------------------------------
String text = “가나다라마바사아자차카타파아야ABCDEFGHIJKLMNOPdfsgdsdfefsdfsdf.”

exTextView.setMaxLine(2);    //최대 라인 수 정의
 
exTextView.setText(text);        
-------------------------------------------------------------------------------------------------
위 코드를 실행했을때 만약 상위 뷰가 작아서 “”까지 밖에 보여줄 수 밖에  없을 경우
가나다라마바사
아자차카타파...
 
getLineCount()를 쓰면 “”이후 “...”으로 생략된 데이터까지 포함해서 라인 수를 가져옵니다

가나다라마바사
아자차카타파아
야ABCDEFGHIJ
KLMNOPdfsgd

sdfefsdfsdf


exTextView의 라인수 = 5를 가져옵니다.

728x90
반응형


public static int getStatusBarHeight(Context context) {

   int result = 0;

   int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");

   if (resourceId > 0) {

       result = context.getResources().getDimensionPixelSize(resourceId);

   }

   return result;

}

728x90
반응형

'Android' 카테고리의 다른 글

android SHA1 알아내기  (0) 2016.10.19
Android TextView Line 가져오기  (0) 2016.09.21
안드로이드 코드에서 마진 주기  (0) 2016.06.22
안드로이드 Px to Dip  (0) 2016.06.22
안드로이드 스크린 사이즈 가져오기  (0) 2016.06.22

+ Recent posts